Skip to content

Commit

Permalink
Added 'window_background_color' setting and fixing keyboard a - z map…
Browse files Browse the repository at this point in the history
…ping in crescent python api (#227)
  • Loading branch information
Chukobyte authored Aug 10, 2024
1 parent 6fb5429 commit 0904479
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 131 deletions.
2 changes: 1 addition & 1 deletion Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (NOT TARGET seika)
FetchContent_Declare(
seika_content
GIT_REPOSITORY https://github.com/Chukobyte/seika.git
GIT_TAG v0.1.4
GIT_TAG v0.1.5
)
FetchContent_MakeAvailable(seika_content)
endif ()
Expand Down
52 changes: 26 additions & 26 deletions crescent_py_api/pocketpy/crescent.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,32 +667,32 @@ def get_world_position() -> Vector2:
class Keyboard:
TAB = 29

A = 97
B = 98
C = 99
D = 100
E = 101
F = 102
G = 103
H = 104
I = 105
J = 106
K = 107
L = 108
M = 109
N = 110
O = 111
P = 112
Q = 113
R = 114
S = 115
T = 116
U = 117
V = 118
W = 119
X = 120
Y = 121
Z = 122
A = 96
B = 97
C = 98
D = 99
E = 100
F = 101
G = 102
H = 103
I = 104
J = 105
K = 106
L = 107
M = 108
N = 109
O = 110
P = 111
Q = 112
R = 113
S = 114
T = 115
U = 116
V = 117
W = 118
X = 119
Y = 120
Z = 121

LEFT = 30
RIGHT = 31
Expand Down
74 changes: 17 additions & 57 deletions editor/src/core/file_creation/scene_file_creator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,16 @@
#include "../utils/json_helper.h"

namespace {
nlohmann::ordered_json Vector2ToJson(SkaVector2 value) {
nlohmann::ordered_json vec;
vec["x"] = value.x;
vec["y"] = value.y;
return vec;
}

template<typename T = SkaSize2D>
nlohmann::ordered_json Size2DToJson(T value) {
nlohmann::ordered_json size;
size["w"] = value.w;
size["h"] = value.h;
return size;
}

nlohmann::ordered_json Rect2ToJson(const SkaRect2& value) {
nlohmann::ordered_json rect;
rect["x"] = value.x;
rect["y"] = value.y;
rect["w"] = value.w;
rect["h"] = value.h;
return rect;
}

nlohmann::ordered_json ColorToJson(const SkaColor& value) {
nlohmann::ordered_json rect;
rect["r"] = (int) (value.r * 255);
rect["g"] = (int) (value.g * 255);
rect["b"] = (int) (value.b * 255);
rect["a"] = (int) (value.a * 255);
return rect;
}

nlohmann::ordered_json MinMaxVector2ToJson(const SkaMinMaxVec2& value) {
nlohmann::ordered_json minmaxVec2;
minmaxVec2["min"] = Vector2ToJson(value.min);
minmaxVec2["max"] = Vector2ToJson(value.max);
return minmaxVec2;
}

nlohmann::ordered_json GetComponentsJsonArray(SceneNode* sceneNode) {
nlohmann::ordered_json componentsJsonArray = nlohmann::ordered_json::array();
if (const auto transform2DComp = sceneNode->GetComponentSafe<Transform2DComp>()) {
nlohmann::ordered_json transform2dJson;
transform2dJson["type"] = "transform_2d";
if (transform2DComp->transform2D.position.x != 0.0f || transform2DComp->transform2D.position.y != 0.0f) {
transform2dJson["position"] = Vector2ToJson(transform2DComp->transform2D.position);
transform2dJson["position"] = JsonHelper::Vector2ToJson(transform2DComp->transform2D.position);
}
if (transform2DComp->transform2D.scale.x != 1.0f || transform2DComp->transform2D.scale.y != 1.0f) {
transform2dJson["scale"] = Vector2ToJson(transform2DComp->transform2D.scale);
transform2dJson["scale"] = JsonHelper::Vector2ToJson(transform2DComp->transform2D.scale);
}
if (transform2DComp->transform2D.rotation != 0.0f) {
transform2dJson["rotation"] = transform2DComp->transform2D.rotation;
Expand All @@ -75,12 +35,12 @@ nlohmann::ordered_json GetComponentsJsonArray(SceneNode* sceneNode) {
nlohmann::ordered_json spriteJson;
spriteJson["type"] = "sprite";
spriteJson["texture_path"] = spriteComp->texturePath;
spriteJson["draw_source"] = Rect2ToJson(spriteComp->drawSource);
spriteJson["draw_source"] = JsonHelper::Rect2ToJson(spriteComp->drawSource);
if (spriteComp->origin.x != 0.0f || spriteComp->origin.y != 0.0f) {
spriteJson["origin"] = Vector2ToJson(spriteComp->origin);
spriteJson["origin"] = JsonHelper::Vector2ToJson(spriteComp->origin);
}
if (spriteComp->modulate.r != 1.0f || spriteComp->modulate.g != 1.0f || spriteComp->modulate.b != 1.0f || spriteComp->modulate.a != 1.0f) {
spriteJson["modulate"] = ColorToJson(spriteComp->modulate);
spriteJson["modulate"] = JsonHelper::ColorToJson(spriteComp->modulate);
}
if (spriteComp->flipH != DEFAULT_COMPONENT_SPRITE_FLIP_H) {
spriteJson["flip_h"] = spriteComp->flipH;
Expand All @@ -99,10 +59,10 @@ nlohmann::ordered_json GetComponentsJsonArray(SceneNode* sceneNode) {
animSpriteJson["current_animation_name"] = animatedSpriteComp->currentAnimationName;
animSpriteJson["is_playing"] = animatedSpriteComp->isPlaying;
if (animatedSpriteComp->origin.x != 0.0f || animatedSpriteComp->origin.y != 0.0f) {
animSpriteJson["origin"] = Vector2ToJson(animatedSpriteComp->origin);
animSpriteJson["origin"] = JsonHelper::Vector2ToJson(animatedSpriteComp->origin);
}
if (animatedSpriteComp->modulate.r != 1.0f || animatedSpriteComp->modulate.g != 1.0f || animatedSpriteComp->modulate.b != 1.0f || animatedSpriteComp->modulate.a != 1.0f) {
animSpriteJson["modulate"] = ColorToJson(animatedSpriteComp->modulate);
animSpriteJson["modulate"] = JsonHelper::ColorToJson(animatedSpriteComp->modulate);
}
if (animatedSpriteComp->flipH != DEFAULT_COMPONENT_ANIMATED_SPRITE_FLIP_H) {
animSpriteJson["flip_h"] = animatedSpriteComp->flipH;
Expand Down Expand Up @@ -130,7 +90,7 @@ nlohmann::ordered_json GetComponentsJsonArray(SceneNode* sceneNode) {
nlohmann::ordered_json frameJson;
frameJson["frame"] = frame.frame;
frameJson["texture_path"] = frame.texturePath;
frameJson["draw_source"] = Rect2ToJson(frame.drawSource);
frameJson["draw_source"] = JsonHelper::Rect2ToJson(frame.drawSource);
framesJsonArray.emplace_back(frameJson);
}
animJson["frames"] = framesJsonArray;
Expand All @@ -147,7 +107,7 @@ nlohmann::ordered_json GetComponentsJsonArray(SceneNode* sceneNode) {
}
textLabelJson["text"] = textLabelComp->text;
if (textLabelComp->color.r != 1.0f || textLabelComp->color.g != 1.0f || textLabelComp->color.b != 1.0f || textLabelComp->color.a != 1.0f) {
textLabelJson["color"] = ColorToJson(textLabelComp->color);
textLabelJson["color"] = JsonHelper::ColorToJson(textLabelComp->color);
}
componentsJsonArray.emplace_back(textLabelJson);
}
Expand All @@ -161,26 +121,26 @@ nlohmann::ordered_json GetComponentsJsonArray(SceneNode* sceneNode) {
if (const auto collider2DComp = sceneNode->GetComponentSafe<Collider2DComp>()) {
nlohmann::ordered_json collider2DJson;
collider2DJson["type"] = "collider_2d";
collider2DJson["extents"] = Size2DToJson(collider2DComp->extents);
collider2DJson["extents"] = JsonHelper::Size2DToJson(collider2DComp->extents);
if (collider2DComp->color.r != 1.0f || collider2DComp->color.g != 1.0f || collider2DComp->color.b != 1.0f || collider2DComp->color.a != 1.0f) {
collider2DJson["color"] = ColorToJson(collider2DComp->color);
collider2DJson["color"] = JsonHelper::ColorToJson(collider2DComp->color);
}
componentsJsonArray.emplace_back(collider2DJson);
}
if (const auto colorRectComp = sceneNode->GetComponentSafe<ColorRectComp>()) {
nlohmann::ordered_json colorRectJson;
colorRectJson["type"] = "color_rect";
colorRectJson["size"] = Size2DToJson(colorRectComp->size);
colorRectJson["size"] = JsonHelper::Size2DToJson(colorRectComp->size);
if (colorRectComp->color.r != 1.0f || colorRectComp->color.g != 1.0f || colorRectComp->color.b != 1.0f || colorRectComp->color.a != 1.0f) {
colorRectJson["color"] = ColorToJson(colorRectComp->color);
colorRectJson["color"] = JsonHelper::ColorToJson(colorRectComp->color);
}
componentsJsonArray.emplace_back(colorRectJson);
}
if (const auto parallaxComp = sceneNode->GetComponentSafe<ParallaxComp>()) {
nlohmann::ordered_json parallaxJson;
parallaxJson["type"] = "parallax";
if (parallaxComp->scrollSpeed.x != 0.0f || parallaxComp->scrollSpeed.y != 0.0f) {
parallaxJson["scroll_speed"] = Vector2ToJson(parallaxComp->scrollSpeed);
parallaxJson["scroll_speed"] = JsonHelper::Vector2ToJson(parallaxComp->scrollSpeed);
}
componentsJsonArray.emplace_back(parallaxJson);
}
Expand All @@ -194,10 +154,10 @@ nlohmann::ordered_json GetComponentsJsonArray(SceneNode* sceneNode) {
particles2DJson["amount"] = particles2DComp->amount;
}
if (particles2DComp->initialVelocity.min.x != 0.0f || particles2DComp->initialVelocity.min.y != 0.0f || particles2DComp->initialVelocity.max.x != 0.0f || particles2DComp->initialVelocity.max.y != 0.0f) {
particles2DJson["initial_velocity"] = MinMaxVector2ToJson(particles2DComp->initialVelocity);
particles2DJson["initial_velocity"] = JsonHelper::MinMaxVector2ToJson(particles2DComp->initialVelocity);
}
if (particles2DComp->color.r != 1.0f || particles2DComp->color.g != 1.0f || particles2DComp->color.b != 1.0f || particles2DComp->color.a != 1.0f) {
particles2DJson["color"] = ColorToJson(particles2DComp->color);
particles2DJson["color"] = JsonHelper::ColorToJson(particles2DComp->color);
}
if (particles2DComp->spread != 45.0f) {
particles2DJson["spread"] = particles2DComp->spread;
Expand All @@ -220,7 +180,7 @@ nlohmann::ordered_json GetComponentsJsonArray(SceneNode* sceneNode) {
tilemapJson["texture_path"] = tilemapTexturePath;
const auto& tileSize = tilemapComp->GetTileSize();
if (tileSize.w != 32 || tileSize.h != 32) {
tilemapJson["tile_size"] = Size2DToJson(tileSize);
tilemapJson["tile_size"] = JsonHelper::Size2DToJson(tileSize);
}
nlohmann::ordered_json activeTilesJsonArray = nlohmann::ordered_json::array();
tilemapComp->ForEachActiveTile([&activeTilesJsonArray](const CreTileData* tileData) {
Expand Down
9 changes: 9 additions & 0 deletions editor/src/core/project_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ void ProjectProperties::LoadPropertiesFromConfig(const char* filePath) {
targetFPS = JsonHelper::Get<int>(propertyJson, "target_fps");
areCollidersVisible = JsonHelper::Get<bool>(propertyJson, "colliders_visible");
version = JsonHelper::GetDefault<std::string>(propertyJson, "version", "0.0.1");
if (JsonHelper::HasKey(propertyJson, "window_background_color")) {
const auto windowBackgroundColorJson = JsonHelper::Get<nlohmann::json>(propertyJson, "window_background_color");
const int r = JsonHelper::Get<int>(windowBackgroundColorJson, "r");
const int g = JsonHelper::Get<int>(windowBackgroundColorJson, "g");
const int b = JsonHelper::Get<int>(windowBackgroundColorJson, "b");
const int a = JsonHelper::Get<int>(windowBackgroundColorJson, "a");
windowBackgroundColor = SkaColor{ r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f };
}
assets.SetAssets(JsonHelper::Get<nlohmann::json>(propertyJson, "assets"));
inputs.SetInputs(JsonHelper::Get<nlohmann::json>(propertyJson, "inputs"));
ska_logger_debug("Loading game properties finished");
Expand Down Expand Up @@ -168,6 +176,7 @@ nlohmann::ordered_json ProjectProperties::ToJson() const {
configJson["initial_node_path"] = initialNodePath;
configJson["colliders_visible"] = areCollidersVisible;
configJson["vsync_enabled"] = vsyncEnabled;
configJson["window_background_color"] = JsonHelper::ColorToJson(windowBackgroundColor);
configJson["version"] = version;

// Assets
Expand Down
17 changes: 10 additions & 7 deletions editor/src/core/project_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include <utility>
#include <vector>

#include <seika/audio/audio.h>
#include <seika/math/math.h>

#include "utils/json_helper.h"
#include "utils/singleton.h"
#include "seika/math/math.h"

struct TextureAsset {
TextureAsset() = default;
Expand Down Expand Up @@ -83,14 +85,15 @@ class ProjectProperties : public Singleton<ProjectProperties> {
public:
std::string gameTitle;
std::string initialNodePath;
int windowWidth;
int windowHeight;
int resolutionWidth;
int resolutionHeight;
uint32_t audioWavSampleRate;
int32 windowWidth;
int32 windowHeight;
int32 resolutionWidth;
int32 resolutionHeight;
uint32 audioWavSampleRate = SKA_AUDIO_SOURCE_DEFAULT_WAV_SAMPLE_RATE;
bool maintainAspectRatio = false;
int targetFPS;
int32 targetFPS;
bool areCollidersVisible = false;
SkaColor windowBackgroundColor = {33.0f / 255.0f, 33.0f / 255.0f, 33.0f / 255.0f, 1.0f };
bool vsyncEnabled = false;
std::string version;
ProjectAssets assets;
Expand Down
3 changes: 3 additions & 0 deletions editor/src/core/ui/views/opened_project/menu_bar_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ ImGuiHelper::MenuBar OpenedProjectUI::MenuBar::GetMenuBar() {
static ImGuiHelper::CheckBox areCollidersVisibleCheckBox("Are Colliders Visible", projectProperties->areCollidersVisible);
ImGuiHelper::BeginCheckBox(areCollidersVisibleCheckBox);

static ImGuiHelper::ColorEdit4 windowBackgroundColorEdit("Window Background Color", (float*)&projectProperties->windowBackgroundColor);
ImGuiHelper::BeginColorEdit4(windowBackgroundColorEdit);

static ImGuiHelper::InputText versionText("Version", projectProperties->version);
ImGuiHelper::BeginInputText(versionText);
},
Expand Down
55 changes: 49 additions & 6 deletions editor/src/core/utils/json_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

#include <nlohmann/json.hpp>

#include <seika/math/math.h>
#include <seika/assert.h>


namespace JsonHelper {
// General
template<typename JsonType = nlohmann::json>
inline bool HasKey(JsonType& json, const std::string& key) {
bool HasKey(JsonType& json, const std::string& key) {
try {
json.at(key);
return true;
Expand All @@ -22,7 +22,7 @@ inline bool HasKey(JsonType& json, const std::string& key) {
}

template<typename T, typename JsonType = nlohmann::json>
inline T Get(const JsonType& json, const std::string& key) {
T Get(const JsonType& json, const std::string& key) {
if (HasKey(json, key)) {
return json.at(key);
}
Expand All @@ -31,7 +31,7 @@ inline T Get(const JsonType& json, const std::string& key) {
}

template<typename T, typename JsonType = nlohmann::json>
inline T GetDefault(const JsonType& json, const std::string& key, T defaultValue) {
T GetDefault(const JsonType& json, const std::string& key, T defaultValue) {
if (HasKey(json, key)) {
return json.at(key);
}
Expand All @@ -52,19 +52,62 @@ inline JsonType LoadFile(const std::string& filePath) {
}

template<typename JsonType = nlohmann::json>
inline void SaveFile(const std::string& filePath, const JsonType& outputJson, bool format = true) {
void SaveFile(const std::string& filePath, const JsonType& outputJson, bool format = true) {
std::ofstream myFile(filePath);
const std::string jsonText = format ? outputJson.dump(4) : outputJson.dump();
myFile << jsonText << "\n";
myFile.close();
}

template<typename JsonType = nlohmann::json>
inline nlohmann::json ConvertString(const std::string& jsonString) {
nlohmann::json ConvertString(const std::string& jsonString) {
std::stringstream ss;
ss << jsonString;
JsonType outputJson;
outputJson << ss;
return outputJson;
}

// Type to json helpers

inline nlohmann::ordered_json Vector2ToJson(SkaVector2 value) {
nlohmann::ordered_json vec;
vec["x"] = value.x;
vec["y"] = value.y;
return vec;
}

template<typename T = SkaSize2D>
nlohmann::ordered_json Size2DToJson(T value) {
nlohmann::ordered_json size;
size["w"] = value.w;
size["h"] = value.h;
return size;
}

inline nlohmann::ordered_json Rect2ToJson(const SkaRect2& value) {
nlohmann::ordered_json rect;
rect["x"] = value.x;
rect["y"] = value.y;
rect["w"] = value.w;
rect["h"] = value.h;
return rect;
}

inline nlohmann::ordered_json ColorToJson(const SkaColor& value) {
nlohmann::ordered_json rect;
rect["r"] = (int) (value.r * 255);
rect["g"] = (int) (value.g * 255);
rect["b"] = (int) (value.b * 255);
rect["a"] = (int) (value.a * 255);
return rect;
}

inline nlohmann::ordered_json MinMaxVector2ToJson(const SkaMinMaxVec2& value) {
nlohmann::ordered_json minmaxVec2;
minmaxVec2["min"] = Vector2ToJson(value.min);
minmaxVec2["max"] = Vector2ToJson(value.max);
return minmaxVec2;
}

} // namespace JsonHelper
Loading

0 comments on commit 0904479

Please sign in to comment.