Skip to content

Commit

Permalink
show custom cursor in every canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
irrld committed Oct 9, 2023
1 parent d33483f commit e4b465d
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 53 deletions.
Binary file modified assets/images/teoncreative_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/images/teoncreative_logo_white.png
Binary file not shown.
40 changes: 31 additions & 9 deletions src/app/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,41 @@ gApp::~gApp() {
}

void gApp::setup() {
RenderUtil::Setup();
// auto* cnv = new GameCanvas(this, CreateRef<ChessConnectionDummy>(), nullptr);
auto* cnv = new LoadingCanvas(this);
appmanager->setCurrentCanvas(cnv);
RenderUtil::Setup();
cursor_ = new gImage();
cursor_->loadImage("cursor.png");
cursor_->setFiltering(2, 2);
cursor_height_ = cursor_->getHeight();
cursor_width_ = 16;
auto* cnv = new LoadingCanvas(this);
appmanager->setCurrentCanvas(cnv);
appmanager->getWindow()->setCursorMode(0x00034002); // GLFW_CURSOR_HIDDEN
appmanager->setTargetFramerate(1000);
}

void gApp::update() {
}

void gApp::stop() {
auto* canvas = appmanager->getCurrentCanvas();
if (canvas != nullptr) {
canvas->hideNotify();
delete canvas;
}
auto* canvas = appmanager->getCurrentCanvas();
if (canvas != nullptr) {
canvas->hideNotify();
delete canvas;
}
}

void gApp::SetCursorPos(int x, int y) {
cursor_pos_x_ = x;
cursor_pos_y_ = y;
}

void gApp::SetCursorType(CursorType type) {
cursor_type_ = type;
}

void gApp::DrawCursor() {
cursor_->drawSub(cursor_pos_x_ - cursor_width_ * 2, cursor_pos_y_ - cursor_height_,
cursor_width_ * 4, cursor_height_ * 4,
cursor_type_ * 16, 0,
cursor_width_, cursor_height_);
}
42 changes: 29 additions & 13 deletions src/app/app.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
/*
* gApp.h
*
* Created on: May 6, 2020
* Author: noyan
*/
* gApp.h
*
* Created on: May 6, 2020
* Author: noyan
*/

#ifndef GAPP_H_
#define GAPP_H_

#include "gBaseApp.h"

enum CursorType {
kArrow,
kHand,
kHandClosed
};

class gApp : public gBaseApp {
public:
gApp();
gApp(int argc, char **argv);
~gApp();
public:
gApp();
gApp(int argc, char **argv);
~gApp();

void setup();
void update();

void stop();

void SetCursorPos(int x, int y);
void SetCursorType(CursorType type);
void DrawCursor();

void setup();
void update();
bool loaded_ = false;

void stop();
private:
gImage* cursor_;
CursorType cursor_type_ = kArrow;
int cursor_width_, cursor_height_;
int cursor_pos_x_ = -100, cursor_pos_y_ = -100;

bool loaded_ = false;
};

#endif /* GAPP_H_ */
26 changes: 7 additions & 19 deletions src/game_canvas.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "game_canvas.h"
#include "message_canvas.h"

GameCanvas::GameCanvas(gApp* root, Ref<ChessConnection> connection, Ref<std::thread> thread) : gBaseCanvas(root), connection_(connection), thread_(thread) {
GameCanvas::GameCanvas(gApp* root, Ref<ChessConnection> connection, Ref<std::thread> thread) : gBaseCanvas(root), root_(root), connection_(connection), thread_(thread) {
this->root_ = root;
}

Expand All @@ -20,10 +20,6 @@ void GameCanvas::setup() {
player_color_ = kPieceColorWhite;
flip_board_ = false;
}
cursor_.loadImage("cursor.png");
cursor_.setFiltering(2, 2);
cursor_height_ = cursor_.getHeight();
cursor_width_ = 16;
board_textures_[0].loadImage("boards/board_persp_01.png");
board_textures_[0].setFiltering(2, 2);
board_textures_[1].loadImage("boards/board_persp_01_flipped.png");
Expand Down Expand Up @@ -83,9 +79,6 @@ void GameCanvas::setup() {
SetInfoText("You lost!");
}
});
appmanager->getWindow()->setCursorMode(0x00034002); // GLFW_CURSOR_HIDDEN
appmanager->setTargetFramerate(120);

connection_->Ready();
}

Expand Down Expand Up @@ -129,10 +122,7 @@ void GameCanvas::draw() {
DrawBoard();
DrawInfoText();
// pos * 2 -> pos * 4 / 2 -> pos * pixelScale / 2
cursor_.drawSub(cursor_pos_x_ - cursor_width_ * 2, cursor_pos_y_ - cursor_height_,
cursor_width_ * 4, cursor_height_ * 4,
cursor_type_ * 16, 0,
cursor_width_, cursor_height_);
root_->DrawCursor();
/*for (int x = 0; x < 8; ++x) {
for (int y = 0; y < 8; ++y) {
RenderUtil::DrawFont(gToStr(x) + " " + gToStr(y),
Expand Down Expand Up @@ -322,8 +312,7 @@ void GameCanvas::charPressed(unsigned int codepoint) {
}

void GameCanvas::mouseMoved(int x, int y) {
cursor_pos_x_ = x;
cursor_pos_y_ = y;
root_->SetCursorPos(x, y);
if (input_lock_ || promoting_ || connection_->GetCurrentTurn() != player_color_) {
return;
}
Expand All @@ -346,18 +335,17 @@ void GameCanvas::mouseMoved(int x, int y) {

void GameCanvas::mouseDragged(int x, int y, int button) {
// gLogi("GameCanvas") << "mouseDragged" << ", x:" << x << ", y:" << y << ", b:" << button;
cursor_pos_x_ = x;
cursor_pos_y_ = y;
cursor_type_ = CursorType::kHandClosed;
root_->SetCursorPos(x, y);
root_->SetCursorType(CursorType::kHandClosed);
}

void GameCanvas::mousePressed(int x, int y, int button) {
// gLogi("GameCanvas") << "mousePressed" << ", x:" << x << ", y:" << y << ", b:" << button;
cursor_type_ = CursorType::kHandClosed;
root_->SetCursorType(CursorType::kHandClosed);
}

void GameCanvas::mouseReleased(int x, int y, int button) {
cursor_type_ = CursorType::kArrow;
root_->SetCursorType(CursorType::kArrow);
if (animate_ || input_lock_ || connection_->GetCurrentTurn() != player_color_) {
return;
}
Expand Down
9 changes: 0 additions & 9 deletions src/game_canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
#include "gImage.h"
#include "net/packets.h"

enum CursorType {
kArrow,
kHand,
kHandClosed
};

class GameCanvas : public gBaseCanvas {
public:
Expand Down Expand Up @@ -50,8 +45,6 @@ class GameCanvas : public gBaseCanvas {
void UpdateFallAnimation();
private:
gApp* root_;
gImage cursor_;
int cursor_width_, cursor_height_;
gImage board_textures_[2];
gImage outliner_;
int board_pos_x, board_pos_y;
Expand Down Expand Up @@ -84,8 +77,6 @@ class GameCanvas : public gBaseCanvas {
bool ended_ = false;
PieceColor winner_color_;
double dance_anim_time_ = 0.0f;
int cursor_pos_x_ = -100, cursor_pos_y_ = -100;
CursorType cursor_type_ = CursorType::kArrow;
private:
void SetInfoText(const std::string& text);
};
2 changes: 1 addition & 1 deletion src/loading_canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LoadingCanvas::~LoadingCanvas() {
}

void LoadingCanvas::setup() {
brand_logo_.loadImage("teoncreative_logo_white.png");
brand_logo_.loadImage("teoncreative_logo.png");
brand_width_ = brand_logo_.getWidth() / 4;
brand_height_ = brand_logo_.getHeight() / 4;
brand_x_ = (getWidth() - brand_width_) / 2;
Expand Down
8 changes: 7 additions & 1 deletion src/menu_canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ void MenuCanvas::setup() {
loading_anim = 0;
title = "";
task = nullptr;
appmanager->setTargetFramerate(120);
fade_in_ = !root->loaded_;
root->loaded_ = true;
fade_in_timer_ = 0.0f;
Expand Down Expand Up @@ -217,6 +216,7 @@ void MenuCanvas::draw() {
}
RenderUtil::DrawFont(title, getWidth() / 2, 150, {1.0f, 1.0f, 1.0f}, true);
setColor(og_color);
root->DrawCursor();
return;
}
// buttons
Expand All @@ -226,6 +226,7 @@ void MenuCanvas::draw() {
// logo
RenderUtil::DrawFont(&logo_font, "Chess Tacos", getWidth() / 2 - logo_font.getStringWidth("Chess Tacos") / 2, 150);
setColor(og_color);
root->DrawCursor();
}

void MenuCanvas::keyPressed(int key) {
Expand All @@ -242,6 +243,7 @@ void MenuCanvas::charPressed(unsigned int codepoint) {

void MenuCanvas::mouseMoved(int x, int y) {
// gLogi("gCanvas") << "mouseMoved" << ", x:" << x << ", y:" << y;
root->SetCursorPos(x, y);
if (loading) {
return;
}
Expand All @@ -252,10 +254,13 @@ void MenuCanvas::mouseMoved(int x, int y) {

void MenuCanvas::mouseDragged(int x, int y, int button) {
// gLogi("gCanvas") << "mouseDragged" << ", x:" << x << ", y:" << y << ", b:" << button;
root->SetCursorPos(x, y);
root->SetCursorType(CursorType::kHandClosed);
}

void MenuCanvas::mousePressed(int x, int y, int button) {
// gLogi("gCanvas") << "mousePressed" << ", x:" << x << ", y:" << y << ", b:" << button;
root->SetCursorType(CursorType::kHandClosed);
if (loading) {
return;
}
Expand All @@ -266,6 +271,7 @@ void MenuCanvas::mousePressed(int x, int y, int button) {

void MenuCanvas::mouseReleased(int x, int y, int button) {
// gLogi("gCanvas") << "mouseReleased" << ", button:" << button;
root->SetCursorType(CursorType::kArrow);
if (loading) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/message_canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void MessageCanvas::draw() {
back_button->Draw();
// logo
RenderUtil::DrawFont(message, getWidth() / 2, 300);
root->DrawCursor();
}

void MessageCanvas::keyPressed(int key) {
Expand Down
2 changes: 1 addition & 1 deletion znet
Submodule znet updated from e4233d to 795c57

0 comments on commit e4b465d

Please sign in to comment.