-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathsmooth_options_menu.cpp
More file actions
166 lines (140 loc) · 4.34 KB
/
Copy pathsmooth_options_menu.cpp
File metadata and controls
166 lines (140 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* @file smooth_options_menu.cpp
* @author Forairaaaaa
* @brief
* @version 0.1
* @date 2025-08-16
*
* @copyright Copyright (c) 2025
*
*/
#include "../utils/raylib_wrapper.hpp"
#include <smooth_ui_toolkit.hpp>
#include <uitk/short_namespace.hpp>
#include <mooncake_log.h>
#include <cmath>
using namespace uitk;
class Menu : public SmoothOptionsMenu {
public:
/**
* @brief Setup menu
*
*/
void init()
{
// Update in every frame is required by raylib
setConfig().renderInterval = 0;
setConfig().readInputInterval = 0;
// Add 5 options
for (int i = 0; i < 5; i++) {
addOption(nullptr);
}
// Configure animation for all options
for (auto& option : setOptionList()) {
// Position animation - smooth and bouncy
option.position.x.springOptions().visualDuration = 0.5;
option.position.x.springOptions().bounce = 0.4;
option.position.y.springOptions() = option.position.x.springOptions();
// Shape animation - slightly slower
option.shape.x.springOptions().visualDuration = 0.6;
option.shape.x.springOptions().bounce = 0.3;
option.shape.y.springOptions() = option.shape.x.springOptions();
}
// Setup circular keyframe positions
float centerX = 400;
float centerY = 225;
float radius = 150;
for (int i = 0; i < 5; i++) {
float angle = (i * 2.0f * M_PI / 5.0f) - M_PI / 2; // Start from top
float x = centerX + radius * std::cos(angle);
float y = centerY + radius * std::sin(angle);
// First keyframe (top) is larger
if (i == 0) {
setKeyframe(i, {x - 60, y - 30, 120, 60});
} else {
setKeyframe(i, {x - 40, y - 20, 80, 40});
}
}
// Initialize to first keyframe
jumpTo(0);
}
void onGoLast() override
{
mclog::info("go last");
}
void onGoNext() override
{
mclog::info("go next");
}
void onPress() override
{
mclog::info("on press");
}
void onRelease() override
{
mclog::info("on release");
}
void onClick() override
{
mclog::info("on click, selected index: {}", getSelectedOptionIndex());
}
/**
* @brief Update input and make action
*
*/
void onReadInput() override
{
// Switch between options
if (IsKeyPressed(KEY_A)) {
goLast();
} else if (IsKeyPressed(KEY_D)) {
goNext();
}
// Press and release selected option
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
// Squeeze the selected option
auto& selected = getSelectedOption();
auto current_frame = getOptionCurrentFrame(getMatchingOptionIndex(0));
auto pressed_keyframe = shape::scale<float>(current_frame, shape::Anchor::Center, {1.3, 0.6});
press(pressed_keyframe);
} else if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
release();
}
}
/**
* @brief Render menu base on all the keyframes
*
*/
void onRender() override
{
// Clear
ClearBackground(WHITE);
// Draw center point
DrawCircle(400, 225, 5, GRAY);
// Render each option with different colors
Color colors[] = {RED, ORANGE, VIOLET, GREEN, BLUE};
for (int i = 0; i < getKeyframeList().size(); i++) {
int option_index = getMatchingOptionIndex(i);
auto frame = getOptionCurrentFrame(option_index);
DrawRectangle(frame.x, frame.y, frame.width, frame.height, colors[option_index]);
DrawText(std::to_string(option_index).c_str(), frame.x + 5, frame.y, 20, WHITE);
}
// Draw keyframe positions (for debugging)
for (int i = 0; i < getKeyframeList().size(); i++) {
auto kf = getKeyframe(i);
DrawRectangleLines(kf.x, kf.y, kf.width, kf.height, LIGHTGRAY);
}
// Instructions
DrawText("Press A/D to navigate", 10, 10, 20, BLACK);
DrawText("Click to select", 10, 35, 20, BLACK);
}
};
int main()
{
Menu menu;
menu.init();
raylib::create_window(800, 450, "你好", [&]() {
menu.update();
});
return 0;
}