Skip to content

Commit 7f9fcf3

Browse files
committed
UI button
1 parent 7d739dd commit 7f9fcf3

File tree

12 files changed

+192
-72
lines changed

12 files changed

+192
-72
lines changed

Engine/Lib/SDL2

Submodule SDL2 updated 185 files

Engine/Lib/glm

Submodule glm updated 1568 files

Engine/Source/CatWare/Graphics/Renderer/Renderer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,15 @@ namespace CatWare
277277

278278
void Renderer::DrawCharacter( Text::Character* character, Vector2D position, unsigned int size, Color color )
279279
{
280-
glm::mat4 projectionMatrix = camera2D->GetProjectionMatrix( );
280+
// glm::mat4 projectionMatrix = camera2D->GetProjectionMatrix( );
281281

282282
textShader->Bind( );
283283
textShader->SetUniform4f( "u_Color", float( color.r ) / 255.0f, float( color.g ) / 255.0f,
284284
float( color.b ) / 255.0f, float( color.a ) / 255.0f );
285285
textShader->SetUniform1i( "u_Texture", 0 );
286286

287-
float xpos = position.x + character->bearing.x * size;
288-
float ypos = position.y * size;
287+
float xpos = ( position.x + character->bearing.x ) * size;
288+
float ypos = ( position.y ) * size;
289289

290290
float w = character->size.x * size;
291291
float h = character->size.y * size;

Engine/Source/CatWare/Graphics/Window.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ namespace CatWare
6060
{
6161
SDL_Event e;
6262

63-
SDL_PumpEvents( );
64-
6563
while ( SDL_PollEvent( &e ) )
6664
{
6765
ImGui_ImplSDL2_ProcessEvent( &e );
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "Widgets.h"
2+
3+
#include "../Input/KeyboardAndMouse.h"
4+
#include "CatWare/Graphics/Renderer/Renderer.h"
5+
6+
namespace CatWare
7+
{
8+
namespace UI
9+
{
10+
bool Widget::IsHovered( )
11+
{
12+
Vector2D mousePos = Input::GetMouseMotion( );
13+
14+
return (
15+
( mousePos.x > position.x && mousePos.y > position.y ) &&
16+
( mousePos.x < position.x + size.x && mousePos.y < position.y + size.y ) );
17+
}
18+
19+
Button::Button( std::string text )
20+
{
21+
this->text = text;
22+
this->onPress = nullptr;
23+
}
24+
25+
Button::Button( std::string text, void ( *onPress )( ) )
26+
{
27+
this->text = text;
28+
this->onPress = onPress;
29+
}
30+
31+
void Button::Draw( )
32+
{
33+
Renderer::DrawRect( position, size, Style::buttonBorderColor );
34+
35+
Vector2D innerRectPos = position + Style::buttonBorderWidth;
36+
Vector2D innerRectSize = size - Style::buttonBorderWidth * 2;
37+
38+
if ( IsHovered( ) )
39+
Renderer::DrawRect( innerRectPos, innerRectSize, Style::buttonHoveredBG );
40+
else
41+
Renderer::DrawRect( innerRectPos, innerRectSize, Style::buttonBG );
42+
43+
Vector2D textPosition = position;
44+
45+
// casting these down to an int prevents some artifacting
46+
textPosition.y = ( int ) ( position.y + size.y / 2 - Style::font->GetSize( ) / 2 );
47+
textPosition.x = ( int ) ( position.x + size.x / 2 - Style::font->GetStringSize( text ) / 2 );
48+
49+
Renderer::DrawString( text, textPosition, 1, Style::font, Style::buttonText );
50+
}
51+
52+
void Button::Update( )
53+
{
54+
if ( IsHovered( ) && Input::IsMousePressed( 1 ) && onPress != nullptr )
55+
{
56+
if ( !pressedLastFrame )
57+
onPress( );
58+
59+
pressedLastFrame = true;
60+
}
61+
else
62+
pressedLastFrame = false;
63+
}
64+
} // namespace UI
65+
} // namespace CatWare

Engine/Source/CatWare/UI/Widgets.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#pragma once
2+
3+
#include "CatWare/Core.h"
4+
#include "CatWare/Graphics/Text.h"
5+
#include "CatWare/Types/Color.h"
6+
#include "CatWare/Types/Vector.h"
7+
8+
namespace CatWare
9+
{
10+
namespace UI
11+
{
12+
class Style
13+
{
14+
public:
15+
// Text
16+
inline static Text::Font* font = nullptr;
17+
18+
// buttons
19+
inline static Color buttonBG = { 40, 40, 40, 255 };
20+
inline static Color buttonHoveredBG = { 80, 80, 80, 255 };
21+
inline static Color buttonText = { 255, 255, 255, 255 };
22+
inline static int buttonBorderWidth = 2;
23+
inline static Color buttonBorderColor = { 255, 255, 255, 255 };
24+
};
25+
26+
class CATWARE_API Widget
27+
{
28+
public:
29+
Vector2D position;
30+
Vector2D size;
31+
32+
virtual void Draw( ) = 0;
33+
virtual void Update( ) = 0;
34+
35+
virtual void OnPress( UInt8 button ) {}
36+
virtual void OnType( char character, int keycode ) {}
37+
38+
bool IsHovered( );
39+
};
40+
41+
class CATWARE_API Button : public Widget
42+
{
43+
public:
44+
std::string text;
45+
void ( *onPress )( ) = nullptr;
46+
bool pressedLastFrame = false;
47+
48+
Button( std::string text );
49+
50+
Button( std::string text, void ( *onPress )( ) );
51+
void Draw( ) override;
52+
void Update( ) override;
53+
};
54+
} // namespace UI
55+
} // namespace CatWare

TestScript/Source/Main.cpp

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <CatWare.h>
22

3+
#include "CatWare/UI/Widgets.h"
4+
35
using namespace CatWare;
46

57
int i = 1;
@@ -15,14 +17,11 @@ class TestEntity : public Entity
1517

1618
TextureRef texture = TextureRef( "gato.jpg" );
1719

18-
TestEntity( )
19-
{
20-
className = "testEntity";
21-
}
20+
TestEntity( ) { className = "testEntity"; }
2221

2322
void Init( ) override
2423
{
25-
pBody = new DynamicBody( 10, 1, new AABBCollider( transform.position, transform.size ) );
24+
pBody = new DynamicBody( 10, 1, new AABBCollider( transform.position, transform.size ) );
2625
pBody->position = transform.position;
2726
SceneManager::GetScene( )->world.physicsWorld.AddBody( pBody );
2827

@@ -77,34 +76,29 @@ class TestEntity : public Entity
7776
emmiter.Emit( );
7877
}
7978

80-
void Draw( )
81-
{
82-
Renderer::DrawRectTextured( transform, texture.Get( ) );
83-
}
79+
void Draw( ) { Renderer::DrawRectTextured( transform, texture.Get( ) ); }
8480

85-
CW_ENTITY_CREATE( tags )
86-
{
87-
return new TestEntity( );
88-
}
81+
CW_ENTITY_CREATE( tags ) { return new TestEntity( ); }
8982
};
9083

84+
void OnPress( ) { CW_LOG->Error( "Pressed a button" ); }
85+
9186
class InGame : public Scene
9287
{
9388
public:
89+
UI::Button button = UI::Button( "Hello world", &OnPress );
90+
9491
InGame( )
9592
{
9693
world.physicsWorld.gravity = 400;
97-
}
9894

99-
void OnEnter( ) override
100-
{
101-
world.LoadFromMapFile( "testMap.yaml" );
95+
button.position = { 20, 20 };
96+
button.size = { 300, 100 };
10297
}
10398

104-
void Update( ) override
105-
{
99+
void OnEnter( ) override { world.LoadFromMapFile( "testMap.yaml" ); }
106100

107-
}
101+
void Update( ) override {}
108102

109103
void Tick( ) override
110104
{
@@ -124,15 +118,17 @@ class InGame : public Scene
124118
}
125119
}
126120

127-
void Draw( ) override
128-
{
129-
Renderer::Clear( { 40, 40, 40, 255 } );
130-
}
121+
void Draw( ) override { Renderer::Clear( { 40, 40, 40, 255 } ); }
131122

132123
void DrawGUI( ) override
133124
{
134-
Renderer::DrawString( "FPS: " + std::to_string( 1.0 / Time::GetDeltaTime( ) ), { 22, 22 }, 1, font, { 0, 0, 0, 255 } );
135-
Renderer::DrawString( "FPS: " + std::to_string( 1.0 / Time::GetDeltaTime( ) ), { 20, 20 }, 1, font, { 255, 255, 255, 255 } );
125+
Renderer::DrawString(
126+
"FPS: " + std::to_string( 1.0 / Time::GetDeltaTime( ) ), { 22, 22 }, 1, font, { 0, 0, 0, 255 } );
127+
Renderer::DrawString(
128+
"FPS: " + std::to_string( 1.0 / Time::GetDeltaTime( ) ), { 20, 20 }, 1, font, { 255, 255, 255, 255 } );
129+
130+
button.Draw( );
131+
button.Update( );
136132
}
137133
};
138134

@@ -141,7 +137,7 @@ InGame* inGame;
141137
class Game : Script
142138
{
143139
public:
144-
void PreInit(CatWare::InitConfig* initConfig) override
140+
void PreInit( CatWare::InitConfig* initConfig ) override
145141
{
146142
initConfig->windowWidth = 1920;
147143
initConfig->windowHeight = 1080;
@@ -151,14 +147,15 @@ class Game : Script
151147
Time::modifier = 1.0;
152148
}
153149

154-
void Start() override
150+
void Start( ) override
155151
{
156152
font = new Text::Font( "EngineRes/Fonts/Jura-Regular.ttf", 30 );
153+
UI::Style::font = font;
157154

158155
Time::frameRateLimited = false;
159156
Time::maxFPS = 240;
160157

161-
EntityRegistry::RegisterEntity<TestEntity>( "testEntity" );
158+
EntityRegistry::RegisterEntity< TestEntity >( "testEntity" );
162159

163160
TextureManager::AddTexture( "gato.jpg", Rendering::TextureFilter::LINEAR );
164161
TextureManager::AddTexture( "whitePuff00.png", Rendering::TextureFilter::LINEAR );
@@ -169,15 +166,9 @@ class Game : Script
169166
SceneManager::SetScene( inGame );
170167
}
171168

172-
void Activate() override
173-
{
174-
175-
}
169+
void Activate( ) override {}
176170

177-
void Exit() override
178-
{
179-
delete font;
180-
}
171+
void Exit( ) override { delete font; }
181172
};
182173

183-
CW_REGISTER_SCRIPT( Game );
174+
CW_REGISTER_SCRIPT( Game );

WorkDir/engine.log

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
[Engine][Info] Creating 1920x1080 window with title CatWare
44
[Engine][Info] Initializing OpenGL
55
[Engine][Info] Vendor: AMD
6-
[Engine][Info] Renderer: AMD Radeon Graphics (radeonsi, renoir, LLVM 17.0.6, DRM 3.54, 6.6.11-200.fc39.x86_64)
7-
[Engine][Info] Version: 4.6 (Core Profile) Mesa 23.3.3
6+
[Engine][Info] Renderer: AMD Radeon Graphics (radeonsi, renoir, LLVM 17.0.6, DRM 3.57, 6.7.4-200.fc39.x86_64)
7+
[Engine][Info] Version: 4.6 (Core Profile) Mesa 23.3.5
88
[Engine][Info] Initializing renderer
99
[Engine][Info] Initializing renderer
1010
[Engine][Info] Loading shader from files:
@@ -19,10 +19,6 @@
1919
[Engine][Info] Loading map from testMap.yaml
2020
[Engine][Info] Loading texture gato.jpg
2121
[Engine][Info] Loading texture whitePuff01.png
22-
[Engine][Info] Loading texture whitePuff02.png
2322
[Engine][Info] Loading texture whitePuff00.png
24-
[Engine][Info] Unloading texture gato.jpg
25-
[Engine][Info] Unloading texture whitePuff02.png
26-
[Engine][Info] Unloading texture whitePuff01.png
27-
[Engine][Info] Unloading texture whitePuff00.png
23+
[Engine][Info] Loading texture whitePuff02.png
2824
[Engine][Info] Closing window

0 commit comments

Comments
 (0)