GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
abstract_dxlib_gui.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
9
10#include <string>
11
12#include <DxLib.h>
13
14#include "font_loader.h"
15
16
17namespace designlab
18{
19
20AbstractDxlibGui::AbstractDxlibGui(int width, int height) :
21 width_(width),
22 height_(height),
23 font_handle_(FontLoader::GetIns()->GetFontHandle("font/Yu_Gothic_UI.dft"))
24{
25}
26
28 const int pos_x, const int pos_y,
29 const unsigned int option, const bool this_is_first_time)
30{
31 const int past_x = gui_left_pos_x_;
32 const int past_y = gui_top_pos_y_;
33
34 if (option & kDxlibGuiAnchorLeft)
35 {
36 gui_left_pos_x_ = pos_x;
37 }
38 else if (option & kDxlibGuiAnchorMiddleX)
39 {
40 gui_left_pos_x_ = pos_x - width_ / 2;
41 }
42 else if (option & kDxlibGuiAnchorRight)
43 {
44 gui_left_pos_x_ = pos_x - width_;
45 }
46
47 if (option & kDxlibGuiAnchorTop)
48 {
49 gui_top_pos_y_ = pos_y;
50 }
51 else if (option & kDxlibGuiAnchorMiddleY)
52 {
53 gui_top_pos_y_ = pos_y - height_ / 2;
54 }
55 else if (option & kDxlibGuiAnchorBottom)
56 {
57 gui_top_pos_y_ = pos_y - height_;
58 }
59
60 const int diff_x = gui_left_pos_x_ - past_x;
61 const int diff_y = gui_top_pos_y_ - past_y;
62
63 for (auto& button : button_)
64 {
65 button->SetPos(button->GetPosMiddleX() + diff_x,
66 button->GetPosMiddleY() + diff_y,
68 }
69
70 if (this_is_first_time)
71 {
74 }
75}
76
78{
79 visible_ = visible;
80
81 for (auto& button : button_)
82 {
83 button->SetVisible(visible);
84 }
85
86 if (visible)
87 {
89 }
90}
91
93{
94 // 各ボタンの処理.
95 for (auto& button : button_)
96 {
97 if (button->CursorOnGui(state.cursor_x, state.cursor_y))
98 {
99 button->ClickedAction(state);
100 }
101 }
102}
103
105 const int cursor_x, const int cursor_y) const noexcept
106{
107 if (!IsVisible())
108 {
109 return false;
110 }
111
112 return gui_left_pos_x_ < cursor_x && cursor_x < gui_left_pos_x_ + width_ &&
113 gui_top_pos_y_ < cursor_y && cursor_y < gui_top_pos_y_ + height_;
114}
115
116bool AbstractDxlibGui::IsDraggable(int cursor_x, int cursor_y) const
117{
118 if (!IsVisible())
119 {
120 return false;
121 }
122
123 return CursorOnGui(cursor_x, cursor_y);
124}
125
127 int cursor_dif_x, int cursor_dif_y,
128 unsigned int mouse_key_bit)
129{
130 if (!(mouse_key_bit & MOUSE_INPUT_LEFT))
131 {
132 return;
133 }
134
135 SetPos(gui_left_pos_x_ + cursor_dif_x,
136 gui_top_pos_y_ + cursor_dif_y,
138}
139
140void AbstractDxlibGui::DrawBackground(const std::string& str) const
141{
142 const unsigned int base_color = GetColor(255, 255, 255);
143 const unsigned int frame_color = GetColor(30, 30, 30);
144 const unsigned int alpha = 200;
145
146 const int frame_width = 1;
147
148 SetDrawBlendMode(DX_BLENDMODE_ALPHA, alpha);
149
150 DrawBox(gui_left_pos_x_ - frame_width,
151 gui_top_pos_y_ - frame_width,
152 gui_left_pos_x_ + width_ + frame_width,
153 gui_top_pos_y_ + height_ + frame_width,
154 frame_color, TRUE);
155 DrawBox(gui_left_pos_x_,
159 base_color, TRUE);
160
161 DrawBox(gui_left_pos_x_,
165 base_color, TRUE);
166 DrawBox(gui_left_pos_x_ - frame_width,
167 gui_top_pos_y_ - frame_width,
168 gui_left_pos_x_ + width_ + frame_width,
169 gui_top_pos_y_ + kTitleBarHeight + frame_width,
170 frame_color, FALSE);
171
172
173 const int text_pos_x = gui_left_pos_x_ + 10;
174 const int text_pos_y = gui_top_pos_y_ + 10;
175 const unsigned int text_color = GetColor(10, 10, 10);
176 DrawFormatStringToHandle(
177 text_pos_x, text_pos_y, text_color, font_handle_, str.c_str());
178
179 SetDrawBlendMode(DX_BLENDMODE_NOBLEND, 0);
180}
181
182} // namespace designlab
int init_pos_y_
SetされたGUIの左上のY座標.
void SetPos(int pos_x, int pos_y, unsigned int option=kDxlibGuiAnchorLeftTop, bool this_is_first_time=false)
GUIの位置を設定する. Dxlibの画面の座標は左上を原点とし,右下に行くほど値が大きくなる. 横方向にx軸,縦方向にy軸をとる.
void DrawBackground(const std::string &str) const
bool visible_
GUIが表示されているかどうかのフラグ.
int gui_left_pos_x_
GUIの左端の位置.
static constexpr int kTitleBarHeight
タイトルバーの高さ.
const int width_
GUIの横幅.
int gui_top_pos_y_
GUIの上端の位置.
void SetVisible(bool visible) override
GUIの表示を行うかどうかを設定する.
void DraggedAction(int cursor_dif_x, int cursor_dif_y, unsigned int mouse_key_bit) override
ドラッグ中の処理を行う. カーソルは上にあるけど,ドラッグ中ではない場合でも呼び出される.
bool IsDraggable(int cursor_x, int cursor_y) const override
ドラッグ可能な位置にあるかを判定する.
constexpr bool IsVisible() const override
GUIの表示を行うかどうかを返す.
bool CursorOnGui(int cursor_x, int cursor_y) const noexcept override
GUIの上にカーソルがあるかどうかを返す.
int init_pos_x_
SetされたGUIの左上のX座標.
int font_handle_
フォントハンドル.
std::vector< std::unique_ptr< SimpleButton > > button_
ボタンのリスト.
const int height_
GUIの縦幅.
void ClickedAction(const DxlibMouseState &state) override
GUIがクリックされたときに実行される関数. CursorOnGuiが true を返すときに呼び出される. 複数のGUIが重なっている場合は,優先度の高いものから順に呼び出される.
Dxlibのフォントを読み込むクラス.
Definition font_loader.h:32
constexpr unsigned int kDxlibGuiAnchorTop
constexpr unsigned int kDxlibGuiAnchorMiddleX
constexpr unsigned int kDxlibGuiAnchorBottom
constexpr unsigned int kDxlibGuiAnchorMiddleXMiddleY
constexpr unsigned int kDxlibGuiAnchorLeft
constexpr unsigned int kDxlibGuiAnchorRight
constexpr unsigned int kDxlibGuiAnchorLeftTop
constexpr unsigned int kDxlibGuiAnchorMiddleY
マウスの状態を表す構造体.
int cursor_y
マウスカーソルのY座標.上端を0とし,下に正の値をとる.
int cursor_x
マウスカーソルのX座標.左端を0とし,右に正の値をとる.