// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/gapsview/gapsview.cpp
// Function: GLUTMouse(int, int, int, int)
static void
GLUTMouse(int button, int state, int x, int y)
{
  // Invert y coordinate
  y = GLUTwindow_height - y;
  
  // Process mouse button event
  if (state == GLUT_DOWN) {
    // Button is going down
    GLUTmouse_drag_distance_squared = 0;

    // Process thumbwheel
    if ((button == 3) || (button == 4)) {
      R3Point viewing_center_point = world_origin;
      const R3Camera& camera = viewer->Camera();
      R3Plane camera_plane(camera.Origin(), camera.Towards());
      RNScalar signed_distance = R3SignedDistance(camera_plane, viewing_center_point);
      if (signed_distance < 0) viewing_center_point -= (signed_distance - 1) * camera.Towards();
      if (button == 3) viewer->ScaleWorld(viewing_center_point, 1.1);
      else if (button == 4) viewer->ScaleWorld(viewing_center_point, 0.9);
    }
  }
  else { // Button is going up
    // Check for drag
    RNBoolean drag = (GLUTmouse_drag_distance_squared > 10 * 10);
    GLUTmouse_drag_distance_squared = 0;

    // Process button 
    if (button == GLUT_LEFT) {
      // Check for double click  
      // static RNBoolean double_click = FALSE;
      // static RNTime last_mouse_down_time;
      // double_click = (!double_click) && (last_mouse_down_time.Elapsed() < 0.4);
      // last_mouse_down_time.Read();

      // Select position
      if (!drag) {
        int e;
        R3Point p;
        if (Pick(x, y, &p, NULL, &e)) {
          world_origin = p;
          if ((e != GSV_CAMERA_ELEMENT_TYPE) && (e != SFL_CAMERA_ELEMENT_TYPE)) {
            selected_position = p;
          }
        }
      }
    }
  }
  
  // Remember button state 
  int b = (button == GLUT_LEFT_BUTTON) ? 0 : ((button == GLUT_MIDDLE_BUTTON) ? 1 : 2);
  GLUTbutton[b] = (state == GLUT_DOWN) ? 1 : 0;

  // Remember modifiers 
  GLUTmodifiers = glutGetModifiers();

  // Remember mouse position 
  GLUTmouse[0] = x;
  GLUTmouse[1] = y;

  // Redraw
  glutPostRedisplay();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movl %edx, %r12d
movl %esi, %r15d
movl %edi, %r14d
movl 0x1a789b(%rip), %ebp    # 0x1d59f4
subl %ecx, %ebp
testl %esi, %esi
je 0x2e3b9
movl 0x1ad267(%rip), %eax    # 0x1db3d0
movl $0x0, 0x1ad25d(%rip)    # 0x1db3d0
testl %r14d, %r14d
jne 0x2e4aa
cmpl $0x64, %eax
jg 0x2e4aa
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
callq 0xd43dc
leaq 0x5b1597(%rip), %rsi    # 0x5df730
movq %rbx, %rdi
callq 0xd4746
movss 0x11df83(%rip), %xmm3   # 0x14c12c
xorps %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x27ea0
movl $0x4100, %edi           # imm = 0x4100
callq 0x271f0
movq 0x1acfa7(%rip), %rdi    # 0x1db170
addq $0x8, %rdi
xorl %esi, %esi
callq 0x8b048
movl $0xb50, %edi            # imm = 0xB50
callq 0x27200
movl $0x809d, %edi           # imm = 0x809D
callq 0x27200
movss 0x11df44(%rip), %xmm0   # 0x14c134
callq 0x270a0
movss 0x11df37(%rip), %xmm0   # 0x14c134
callq 0x281d0
movl $0x5, %edi
callq 0x2e5b6
movl $0x809d, %edi           # imm = 0x809D
callq 0x27880
movss 0x11df0e(%rip), %xmm0   # 0x14c12c
callq 0x270a0
movss 0x11df01(%rip), %xmm0   # 0x14c12c
callq 0x281d0
callq 0x283a0
leaq 0x14(%rsp), %rbx
movq %rbx, (%rsp)
movl %r12d, %edi
movl %ebp, %esi
movl $0x1, %edx
movl $0x1, %ecx
movl $0x1908, %r8d           # imm = 0x1908
movl $0x1401, %r9d           # imm = 0x1401
callq 0x273d0
movzbl 0x3(%rbx), %r13d
testl %r13d, %r13d
je 0x2e4aa
leal 0x1(%r13), %eax
cmpb $0x11, %al
jb 0x2e4aa
andl $0xf, %r13d
je 0x2e4aa
movzwl 0x14(%rsp), %eax
movzbl 0x16(%rsp), %ecx
orl %eax, %ecx
je 0x2e4aa
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x40(%rsp)
decl %r13d
leaq 0x50(%rsp), %rsi
movl $0xba2, %edi            # imm = 0xBA2
callq 0x278e0
leaq 0x110(%rsp), %rsi
movl $0xba6, %edi            # imm = 0xBA6
callq 0x27ab0
leaq 0x90(%rsp), %rbx
movl $0xba7, %edi            # imm = 0xBA7
movq %rbx, %rsi
callq 0x27ab0
movl %r12d, 0x10(%rsp)
leaq 0x4c(%rsp), %r12
movq %r12, (%rsp)
movl 0x10(%rsp), %edi
movl %ebp, %esi
movl $0x1, %edx
movl $0x1, %ecx
movl $0x1902, %r8d           # imm = 0x1902
movl $0x1406, %r9d           # imm = 0x1406
callq 0x273d0
cvtss2sd (%r12), %xmm2
leaq 0x28(%rsp), %r8
leaq 0x30(%rsp), %r9
leaq 0x20(%rsp), %r12
movsd 0x18(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
leaq 0x110(%rsp), %rdi
movq %rbx, %rsi
leaq 0x50(%rsp), %rdx
movq %r12, %rcx
callq 0x282b0
movsd (%r12), %xmm0
movsd 0x8(%r12), %xmm1
movsd 0x10(%r12), %xmm2
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
callq 0xd43de
leaq 0x60(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0xd4746
leaq 0x1ac9a8(%rip), %rdi    # 0x1dad30
movq %r12, %rsi
movl 0x10(%rsp), %r12d
callq 0xd4746
andl $-0x3, %r13d
cmpl $0x8, %r13d
je 0x2e4aa
leaq 0x1ac99e(%rip), %rdi    # 0x1dad48
leaq 0x60(%rsp), %rsi
callq 0xd4746
jmp 0x2e4aa
movl $0x0, 0x1ad00d(%rip)    # 0x1db3d0
leal -0x3(%r14), %eax
cmpl $0x1, %eax
ja 0x2e4aa
movl %r12d, 0x10(%rsp)
leaq 0x1ac954(%rip), %rsi    # 0x1dad30
leaq 0x90(%rsp), %rdi
callq 0xd43ee
movq 0x1acd80(%rip), %r12    # 0x1db170
leaq 0x8(%r12), %r13
addq $0x50, %r12
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0xd5aac
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x20(%rsp), %rdx
callq 0xd2f5e
movq %rbx, %rdi
leaq 0x90(%rsp), %rsi
callq 0x9c387
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2e47c
addsd 0x11dc30(%rip), %xmm0   # 0x14c070
movsd %xmm0, 0x18(%rsp)
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xd5aac
leaq 0x20(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movsd 0x18(%rsp), %xmm0
callq 0xd580e
leaq 0x90(%rsp), %rdi
movq %r12, %rsi
callq 0xd4d46
xorl %eax, %eax
cmpl $0x3, %r14d
sete %al
movq 0x1acce4(%rip), %rdi    # 0x1db170
leaq 0x11dedd(%rip), %rcx    # 0x14c370
movsd (%rcx,%rax,8), %xmm0
leaq 0x90(%rsp), %rsi
callq 0x88e30
movl 0x10(%rsp), %r12d
xorl %eax, %eax
testl %r15d, %r15d
sete %al
xorl %ecx, %ecx
cmpl $0x1, %r14d
setne %cl
incl %ecx
testl %r14d, %r14d
cmovel %r14d, %ecx
leaq 0x1acf09(%rip), %rdx    # 0x1db3d4
movl %eax, (%rdx,%rcx,4)
callq 0x113de1
movl %r12d, 0x1aceee(%rip)   # 0x1db3c8
movl %ebp, 0x1aceec(%rip)    # 0x1db3cc
callq 0x10ef28
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq