GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
simple_button.cpp
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#include "simple_button.h"
9
10#include <algorithm>
11
12#include <Dxlib.h>
13
14#include "cassert_define.h"
15#include "math_util.h"
16#include "string_util.h"
17#include "font_loader.h"
18
19
20namespace designlab
21{
22
24 const std::string& text,
25 const int pos_x, const int pos_y, const int size_x, const int size_y,
26 const bool fit_size) :
27 text_(string_util::Split(text, "\n")),
28 pos_middle_x(pos_x),
29 pos_middle_y(pos_y),
30 kSizeX(fit_size ? GetFitButtonSizeX(size_x) : size_x),
31 kSizeY(fit_size ? GetFitButtonSizeX(size_y) : size_y)
32{
33 assert(0 < kSizeX);
34 assert(0 < kSizeY);
35}
36
38 const int pos_x, const int pos_y, const unsigned int option)
39{
40 if (option & kDxlibGuiAnchorLeft)
41 {
42 pos_middle_x = pos_x - kSizeX / 2;
43 }
44 else if (option & kDxlibGuiAnchorMiddleX)
45 {
46 pos_middle_x = pos_x;
47 }
48 else if (option & kDxlibGuiAnchorRight)
49 {
50 pos_middle_x = pos_x + kSizeX / 2;
51 }
52
53 if (option & kDxlibGuiAnchorTop)
54 {
55 pos_middle_y = pos_y - kSizeY / 2;
56 }
57 else if (option & kDxlibGuiAnchorMiddleY)
58 {
59 pos_middle_y = pos_y;
60 }
61 else if (option & kDxlibGuiAnchorBottom)
62 {
63 pos_middle_y = pos_y + kSizeY / 2;
64 }
65}
66
68{
69 // now_color_blue_を target_color_blue_ に近づける.
70 now_color_blue_ =
71 math_util::ApproachTarget(now_color_blue_, target_color_blue_, 0.1f);
72
73 target_color_blue_ -= 4;
74 target_color_blue_ = target_color_blue_ < 0 ? 0 : target_color_blue_;
75}
76
78{
79 if (!visible_) { return; }
80
81 const int base_color = GetColor(128, 128, 128);
82 const int button_color =
83 GetColor(255 - now_color_blue_, 255 - now_color_blue_ / 2, 255);
84 const int str_color = GetColor(20, 20, 20);
85 const int frame_size = 1;
86
87 // ベースを描画.
88 DrawBox(
89 pos_middle_x - kSizeX / 2,
90 pos_middle_y - kSizeY / 2,
91 pos_middle_x + kSizeX / 2,
92 pos_middle_y + kSizeY / 2,
93 base_color,
94 TRUE);
95
96 // その上にボタンを描画.
97 DrawBox(
98 pos_middle_x - kSizeX / 2 + frame_size,
99 pos_middle_y - kSizeY / 2 + frame_size,
100 pos_middle_x + kSizeX / 2 - frame_size,
101 pos_middle_y + kSizeY / 2 - frame_size,
102 button_color,
103 TRUE);
104
105 // テキストを表示.
106 const int font_handle =
107 FontLoader::GetIns()->GetFontHandle("font/Yu_Gothic_UI.dft");
108
109 for (int i = 0; i < text_.size(); ++i)
110 {
111 const auto text_width = GetDrawStringWidthToHandle(
112 text_[i].c_str(), static_cast<int>(text_[i].size()), font_handle);
113
114 const auto text_height = static_cast<int>(text_.size()) * kFontSize;
115
116 DrawStringToHandle(
117 pos_middle_x - text_width / 2,
118 pos_middle_y - text_height / 2 + i * kFontSize,
119 text_[i].c_str(),
120 str_color,
121 font_handle);
122 }
123}
124
126{
127 target_color_blue_ = 64;
128
129 if (click_function_ &&
130 visible_ &&
131 state.left_pushing_count == 1)
132 {
133 now_color_blue_ = 128;
134
135 click_function_();
136 }
137}
138
139bool SimpleButton::CursorOnGui(const int cursor_x, const int cursor_y) const noexcept
140{
141 return pos_middle_x - kSizeX / 2 < cursor_x &&
142 cursor_x < pos_middle_x + kSizeX / 2 &&
143 pos_middle_y - kSizeY / 2 < cursor_y &&
144 cursor_y < pos_middle_y + kSizeY / 2;
145}
146
147int SimpleButton::GetFitButtonSizeX(const int now_size_x) const noexcept
148{
149 // 文字列の中からもっと横幅が大きいものを探す.
150 const int font_handle =
151 FontLoader::GetIns()->GetFontHandle("font/Yu_Gothic_UI.dft");
152
153 int max_width = 0;
154 for (const auto& i : text_)
155 {
156 const int width = GetDrawStringWidthToHandle(
157 i.c_str(), static_cast<int>(i.size()), font_handle);
158
159 if (max_width < width)
160 {
161 max_width = width;
162 }
163 }
164
165 // 横幅が大きい文字列に合わせてボタンの横幅を変更する.
166 if (now_size_x < max_width)
167 {
168 return max_width;
169 }
170 else
171 {
172 return now_size_x;
173 }
174}
175
176int SimpleButton::GetFitButtonSizeY(const int now_size_y) const noexcept
177{
178 int height = static_cast<int>(text_.size()) * kFontSize;
179
180 if (now_size_y < height)
181 {
182 return height;
183 }
184 else
185 {
186 return now_size_y;
187 }
188}
189
190} // namespace designlab
int GetFontHandle(const std::string &file_path)
Dxlibでは特定のフォントで描画する際に,フォントのハンドルを指定する. この関数では,フォントのファイルパスを指定すると, フォントのハンドル番号を返す. フォントがまだ読み込まれていない場...
void SetPos(int pos_x, int pos_y, unsigned int option=kDxlibGuiAnchorLeftTop)
ボタンの座標を設定する.
void Draw() const override
GUIの描画.
void Update() override
GUIの更新,毎フレーム実行すること.
bool CursorOnGui(int cursor_x, int cursor_y) const noexcept override
GUIの上にカーソルがあるかどうかを返す.
SimpleButton()=delete
デフォルトコンストラクタは生成できない.
void ClickedAction(const DxlibMouseState &state) override
GUIがクリックされたときに実行される関数. CursorOnGuiが true を返すときに呼び出される. 複数のGUIが重なっている場合は,優先度の高いものから順に呼び出される.
static FontLoader * GetIns()
インスタンスを取得する. このクラスを継承したクラスは クラス名::getIns()-> の形式でメンバ関数を呼び出す.
Definition singleton.h:34
T ApproachTarget(const T &current, const T &target, float rate)
目標値に値を近づける関数. 描画用なので,線形でなく,適当に値を近づける. そのため,計算に使いたいなら作り直すこと.
Definition math_util.h:85
constexpr unsigned int kDxlibGuiAnchorTop
constexpr unsigned int kDxlibGuiAnchorMiddleX
constexpr unsigned int kDxlibGuiAnchorBottom
constexpr unsigned int kDxlibGuiAnchorLeft
constexpr unsigned int kDxlibGuiAnchorRight
constexpr unsigned int kDxlibGuiAnchorMiddleY
マウスの状態を表す構造体.
int left_pushing_count
マウスの左ボタンが押されているフレーム数.