Skip to content

Commit 4f459cd

Browse files
committed
add mouse control to satellite scope
1 parent 92b0d44 commit 4f459cd

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/ui.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ static float scope_az = 180.0f;
229229
static float scope_el = 45.0f;
230230
static float scope_beam = 30.0f;
231231

232+
static bool scope_drag_active = false;
233+
static Vector2 scope_drag_last = {0};
234+
232235
static char text_scope_az[16] = "180.0";
233236
static char text_scope_el[16] = "45.0";
234237
static char text_scope_beam[16] = "30.0";
@@ -2730,6 +2733,61 @@ case WND_SCOPE:
27302733
float scope_radius = 160 * cfg->ui_scale;
27312734
Vector2 center = {sc_x + scopeWindow.width / 2.0f, sc_y + 40 * cfg->ui_scale + scope_radius};
27322735

2736+
/* direct mouse control of scope orientation and beam */
2737+
if (is_topmost)
2738+
{
2739+
Vector2 mouse = GetMousePosition();
2740+
float dx = mouse.x - center.x;
2741+
float dy = mouse.y - center.y;
2742+
float dist = sqrtf(dx * dx + dy * dy);
2743+
2744+
/* WHEN NOT LOCKED left-drag inside the viewfinder adjusts az/el */
2745+
if (!scope_lock)
2746+
{
2747+
if (!scope_drag_active && IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && dist <= scope_radius)
2748+
{
2749+
scope_drag_active = true;
2750+
scope_drag_last = mouse;
2751+
}
2752+
if (scope_drag_active && IsMouseButtonDown(MOUSE_LEFT_BUTTON))
2753+
{
2754+
Vector2 cur = mouse;
2755+
Vector2 delta = Vector2Subtract(cur, scope_drag_last);
2756+
scope_drag_last = cur;
2757+
2758+
/* map pixels to angular offsets based on current beam */
2759+
float pix_to_deg = (scope_beam / 2.0f) / scope_radius;
2760+
scope_az += delta.x * pix_to_deg;
2761+
scope_el -= delta.y * pix_to_deg;
2762+
2763+
/* normalize / clamp */
2764+
while (scope_az < 0.0f) scope_az += 360.0f;
2765+
while (scope_az >= 360.0f) scope_az -= 360.0f;
2766+
if (scope_el > 90.0f) scope_el = 90.0f;
2767+
if (scope_el < -90.0f) scope_el = -90.0f;
2768+
2769+
if (!edit_scope_az) snprintf(text_scope_az, sizeof(text_scope_az), "%.1f", scope_az);
2770+
if (!edit_scope_el) snprintf(text_scope_el, sizeof(text_scope_el), "%.1f", scope_el);
2771+
}
2772+
}
2773+
2774+
if (scope_drag_active && IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
2775+
scope_drag_active = false;
2776+
2777+
/* scroll wheel over viewfinder changes beam width */
2778+
if (dist <= scope_radius)
2779+
{
2780+
float wheel = GetMouseWheelMove();
2781+
if (wheel != 0.0f && !edit_scope_beam)
2782+
{
2783+
scope_beam -= wheel * 5.0f;
2784+
if (scope_beam < 1.0f) scope_beam = 1.0f;
2785+
if (scope_beam > 120.0f) scope_beam = 120.0f;
2786+
snprintf(text_scope_beam, sizeof(text_scope_beam), "%.1f", scope_beam);
2787+
}
2788+
}
2789+
}
2790+
27332791
// push state back to context for 3d render
27342792
*ctx->show_scope = true;
27352793
*ctx->scope_az = scope_az;

0 commit comments

Comments
 (0)