Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
Palette position (#15)
Browse files Browse the repository at this point in the history
* Get started with palette position

* Refactor Color Palette for dynamic positioning

* Add scaling

* Fix color palette position

Co-authored-by: Ansh Godha <[email protected]>
  • Loading branch information
foxhatleo and AnshG714 authored Mar 27, 2021
1 parent 62d20e2 commit ad62bc0
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 108 deletions.
6 changes: 6 additions & 0 deletions build-apple/PanicPainter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
EBDD16D525C35D3400154533 /* CoreMotion.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EBDD16C625C35D3400154533 /* CoreMotion.framework */; };
EBDD16D625C35D3400154533 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EBDD16C725C35D3400154533 /* CoreGraphics.framework */; };
EBEC12022194B6F4007E708B /* Metal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EBEC12012194B6F4007E708B /* Metal.framework */; };
EED6D691260CE83C004A2E6F /* PPColorPaletteView.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EED6D690260CE83C004A2E6F /* PPColorPaletteView.cpp */; };
EEFA1A4425F68DA7004641A1 /* PPColorPalette.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EEFA1A4225F68DA7004641A1 /* PPColorPalette.cpp */; };
EEFA1A7125FA816D004641A1 /* PPAnimation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EEFA1A7025FA816D004641A1 /* PPAnimation.cpp */; };
EEFA1A7225FA816D004641A1 /* PPAnimation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EEFA1A7025FA816D004641A1 /* PPAnimation.cpp */; };
Expand Down Expand Up @@ -227,6 +228,8 @@
EBDD16C625C35D3400154533 /* CoreMotion.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreMotion.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk/System/Library/Frameworks/CoreMotion.framework; sourceTree = DEVELOPER_DIR; };
EBDD16C725C35D3400154533 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk/System/Library/Frameworks/CoreGraphics.framework; sourceTree = DEVELOPER_DIR; };
EBEC12012194B6F4007E708B /* Metal.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Metal.framework; path = System/Library/Frameworks/Metal.framework; sourceTree = SDKROOT; };
EED6D690260CE83C004A2E6F /* PPColorPaletteView.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = PPColorPaletteView.cpp; sourceTree = "<group>"; };
EED6D698260CE864004A2E6F /* PPColorPaletteView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PPColorPaletteView.h; sourceTree = "<group>"; };
EEFA1A4225F68DA7004641A1 /* PPColorPalette.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = PPColorPalette.cpp; sourceTree = "<group>"; };
EEFA1A4325F68DA7004641A1 /* PPColorPalette.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PPColorPalette.h; sourceTree = "<group>"; };
EEFA1A6F25FA816D004641A1 /* PPAnimation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PPAnimation.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -384,6 +387,8 @@
C5FB329225F41BCC000694C3 /* PPGameScene.h */,
EEFA1A4225F68DA7004641A1 /* PPColorPalette.cpp */,
EEFA1A4325F68DA7004641A1 /* PPColorPalette.h */,
EED6D690260CE83C004A2E6F /* PPColorPaletteView.cpp */,
EED6D698260CE864004A2E6F /* PPColorPaletteView.h */,
);
path = gameplay;
sourceTree = "<group>";
Expand Down Expand Up @@ -753,6 +758,7 @@
C5621DCF260B8D7400875B72 /* PPCanvasBlock.cpp in Sources */,
EB2BE9B51D74952A002FE78B /* PPApp.cpp in Sources */,
C5FB329F25F41BD1000694C3 /* PPLoadingScene.cpp in Sources */,
EED6D691260CE83C004A2E6F /* PPColorPaletteView.cpp in Sources */,
C5621DC9260B8D7200875B72 /* PPColorStrip.cpp in Sources */,
C5621DD5260B8E3C00875B72 /* PPPauseScene.cpp in Sources */,
EEFA1A7125FA816D004641A1 /* PPAnimation.cpp in Sources */,
Expand Down
6 changes: 5 additions & 1 deletion build-win10/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ set(PP_FILES
../source/scenes/gameplay/PPColorStrip.h
../source/scenes/gameplay/PPColorPalette.cpp
../source/scenes/gameplay/PPColorPalette.h
../source/scenes/gameplay/PPColorPaletteView.cpp
../source/scenes/gameplay/PPColorPaletteView.h
../source/scenes/gameplay/PPGameScene.cpp
../source/scenes/gameplay/PPGameScene.h
../source/scenes/loading/PPLoadingScene.h
Expand All @@ -34,7 +36,9 @@ set(PP_FILES
../source/controllers/PPActionController.h
../source/controllers/PPActionController.cpp
../source/scenes/pause/PPPauseScene.h
../source/scenes/pause/PPPauseScene.cpp ../source/scenes/gameplay/PPGlobalTimer.h ../source/scenes/gameplay/PPGlobalTimer.cpp)
../source/scenes/pause/PPPauseScene.cpp
../source/scenes/gameplay/PPGlobalTimer.h
../source/scenes/gameplay/PPGlobalTimer.cpp)

set(CMAKE_SYSTEM_VERSION 10.0 CACHE STRING "" FORCE)

Expand Down
91 changes: 19 additions & 72 deletions source/scenes/gameplay/PPColorPalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,91 +14,38 @@
#define INACTIVE_SCALE 0.75f
#define PRESSED_SCALE 1.2f

ptr<ColorPalette> ColorPalette::alloc(const Vec2 &pos,

ptr<ColorPalette> ColorPalette::alloc(const Rect &bounds,
const vec<Color4> &colors,
const asset_t &assets) {
auto colorTexture = assets->get<Texture>("color-circle");
auto paletteTexture = assets->get<Texture>("palette");
auto result =
make_shared<ColorPalette>(colors, colorTexture, paletteTexture);
// change to init with texture after changing the header file
if (result->initWithPosition(pos))
result->_setup();
if (result->initWithBounds(bounds))
result->_setup(bounds, colors, assets);
else
return nullptr;
return result;
}

void ColorPalette::_setup() {

int palette_width = 190;
int palette_height = 260;

setAnchor(Vec2::ANCHOR_CENTER);
setContentSize(palette_width, palette_height);

auto bg = PolygonNode::allocWithTexture(_paletteTexture);
bg->setAnchor(Vec2::ANCHOR_BOTTOM_CENTER);
bg->setContentSize(palette_width, palette_height);
addChild(bg);

int x = getWidth() - 35;
int y = getHeight() - 90;
float curvature = 0.15;

for (uint i = 0, j = (uint) _colors.size(); i < j; i++) {
auto btn = PolygonNode::allocWithTexture(_colorTexture);
btn->setContentSize(PALETTE_COLOR_SIZE, PALETTE_COLOR_SIZE);
btn->setAnchor(Vec2::ANCHOR_CENTER);
btn->setPosition(
x - (PADDING + PALETTE_COLOR_SIZE / 2) * i * i * curvature,
y - (PADDING + PALETTE_COLOR_SIZE / 2) * i * PRESSED_SCALE
);
btn->setColor(_colors[i]);
if (i != _selectedColor)
Animation::set(btn, {{"scaleX", INACTIVE_SCALE}, {"scaleY", INACTIVE_SCALE}});
addChild(btn);
_buttons.push_back(btn);
_buttonStates.push_back(i != _selectedColor ? INACTIVE : ACTIVE);
}
}

void ColorPalette::_animateButtonState(uint ind, const ColorButtonState s) {
if (_buttonStates[ind] == s) return;
_buttonStates[ind] = s;
float scale = s == INACTIVE ? INACTIVE_SCALE : (s == PRESSED ? PRESSED_SCALE : 1);
Animation::alloc(_buttons[ind], .2,
{{"scaleX", scale}, {"scaleY", scale}},
STRONG_OUT);
void ColorPalette::_setup(const Rect &bounds,
const vec<Color4> &colors,
const asset_t &assets) {
#ifdef VIEW_DEBUG
auto n = PolygonNode::alloc(Rect(Vec2::ZERO, getContentSize()));
n->setColor(Color4f(0, 0, 1, .3));
addChild(n);
#endif

_paletteView = ColorPaletteView::alloc(colors, assets);
float scale = getWidth() / _paletteView->getWidth();
_paletteView->setScale(scale);
_paletteView->setPositionY(getHeight() / 2);
addChild(_paletteView);
}

void ColorPalette::update() {
auto &input = InputController::getInstance();
if (input.isPressing() || input.justReleased()) {
for (uint i = 0, j = (uint) _colors.size(); i < j; i++) {
auto &btn = _buttons[i];

// Reset scale back to 1 for the purpose of input detection.
Vec2 ns = btn->getScale();
btn->setScale(1, 1);
bool tapped =
InputController::inScene(input.startingPoint(), btn) &&
InputController::inScene(input.currentPoint(), btn);
btn->setScale(ns);

if (!tapped) {
_animateButtonState(i, _selectedColor == i ? ACTIVE : INACTIVE);
continue;
}
if (input.justReleased()) {
_selectedColor = i;
_animateButtonState(i, ACTIVE);
} else {
_animateButtonState(i, PRESSED);
}
}
} else {
for (uint i = 0, j = (uint) _colors.size(); i < j; i++)
_animateButtonState(i, _selectedColor == i ? ACTIVE : INACTIVE);
}
_paletteView->update();
}
45 changes: 11 additions & 34 deletions source/scenes/gameplay/PPColorPalette.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,37 @@
#include "utils/PPHeader.h"
#include "utils/PPAnimation.h"
#include "controllers/PPInputController.h"
#include "PPColorPaletteView.h"

// change to texturedNode
class ColorPalette : public SceneNode {
enum ColorButtonState {
PRESSED,
ACTIVE,
INACTIVE,
};

/** The index of the selected color in the palette. */
uint _selectedColor;

/** The color list. */
const vec<Color4> _colors;

/** Buttons. */
vec<ptr<PolygonNode>> _buttons;

/** Button hover states. */
vec<ColorButtonState> _buttonStates;

/** Color circle texture */
ptr<Texture> _colorTexture;

/** Wooden palette texture */
ptr<Texture> _paletteTexture;

void _setup();
ptr<ColorPaletteView> _paletteView;

void _animateButtonState(uint ind, ColorButtonState s);

void _setup(const Rect &bounds,
const vec<Color4> &colors,
const asset_t &assets);
public:

/** @deprecated Constructor. */
explicit ColorPalette(const vec<Color4> &colors,
const ptr<Texture>& colorTexture,
const ptr<Texture>& paletteTexture) :
SceneNode(), _colors(colors), _selectedColor(0) {
_colorTexture = colorTexture;
_paletteTexture = paletteTexture;
};
const ptr<Texture>& paletteTexture) {};

static ptr<ColorPalette> alloc(const Vec2 &pos,
static ptr<ColorPalette> alloc(const Rect &bounds,
const vec<Color4> &colors,
const asset_t &assets);

/** Set the currently selected color to the appropriate index. */
void setColor(uint colorIndex) {
_selectedColor = colorIndex;
//_selectedColor = colorIndex;
_paletteView->setColor(colorIndex);
}


/** Get the selected color. */
uint getSelectedColor() const {
return _selectedColor;
return _paletteView->getSelectedColor();
}

void update();
Expand Down
136 changes: 136 additions & 0 deletions source/scenes/gameplay/PPColorPaletteView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//
// PPColorPaletteView.cpp
// PanicPainter (Mac)
//
// Created by Ansh Godha on 3/25/21.
// Copyright © 2021 Dragonglass Studios. All rights reserved.
//

#include "PPColorPaletteView.h"

#define PALETTE_COLOR_SIZE 40.0f
/** Space between dots. */
#define PADDING 12.0f
#define INACTIVE_SCALE 0.75f
#define PRESSED_SCALE 1.2f
#define PALETTE_WIDTH 190
#define PALETTE_HEIGHT 260
#define NEGATIVE_MARGIN_LEFT 0.4f /* = 40% of PALETTE_WIDTH */

ptr<ColorPaletteView> ColorPaletteView::alloc(
const vec<Color4> &colors,
const asset_t &assets) {
auto colorTexture = assets->get<Texture>("color-circle");
auto paletteTexture = assets->get<Texture>("palette");
auto result =
make_shared<ColorPaletteView>(colors, colorTexture, paletteTexture);
if (result->init())
result->_setup();
else
return nullptr;
return result;
}

void ColorPaletteView::_setup() {
setAnchor(Vec2::ANCHOR_MIDDLE_LEFT);
setPosition(Vec2::ZERO);

auto bg = PolygonNode::allocWithTexture(_paletteTexture);
bg->setAnchor(Vec2::ANCHOR_BOTTOM_LEFT);
bg->setContentSize(PALETTE_WIDTH, PALETTE_HEIGHT);
bg->setPositionX(-NEGATIVE_MARGIN_LEFT * bg->getContentWidth());
setContentSize(bg->getContentWidth() * (1 - NEGATIVE_MARGIN_LEFT),
bg->getContentHeight());

#ifdef VIEW_DEBUG
auto n = PolygonNode::alloc(Rect(Vec2::ZERO, getContentSize()));
n->setColor(Color4f(1, 0, 0, .3));
addChild(n);
#endif

addChild(bg);

float btnStartX = getContentWidth() - 35;
float btnStartY = getContentHeight() - 90;
float curvature = 0.15;

for (uint i = 0, j = (uint)_colors.size(); i < j; i++) {
auto btn = PolygonNode::allocWithTexture(_colorTexture);
btn->setContentSize(PALETTE_COLOR_SIZE, PALETTE_COLOR_SIZE);
btn->setAnchor(Vec2::ANCHOR_CENTER);
btn->setPosition(
btnStartX - (PADDING + PALETTE_COLOR_SIZE / 2) * i * i * curvature,
btnStartY - (PADDING + PALETTE_COLOR_SIZE / 2) * i * PRESSED_SCALE
);
btn->setColor(_colors[i]);

if (i != _selectedColor) {
Animation::set(
btn,
{
{"scaleX", INACTIVE_SCALE},
{"scaleY", INACTIVE_SCALE}
}
);
}

addChild(btn);
_buttons.push_back(btn);
_buttonStates.push_back(i != _selectedColor ? INACTIVE : ACTIVE);
}
}

void ColorPaletteView::_animateButtonState(uint ind, const ColorButtonState s) {
if (_buttonStates[ind] == s) return;
_buttonStates[ind] = s;

float scale = s == INACTIVE ?
INACTIVE_SCALE :
(s == PRESSED ? PRESSED_SCALE : 1);
Animation::alloc(
_buttons[ind], .2,
{
{"scaleX", scale},
{"scaleY", scale}
},
STRONG_OUT
);
}

void ColorPaletteView::update() {
auto &input = InputController::getInstance();
if (input.isPressing() || input.justReleased()) {
for (uint i = 0, j = (uint) _colors.size(); i < j; i++) {
auto &btn = _buttons[i];

// Reset scale back to 1 for the purpose of input detection.
Vec2 ns = btn->getScale();
btn->setScale(1, 1);
bool tapped =
InputController::inScene(input.startingPoint(), btn) &&
InputController::inScene(input.currentPoint(), btn);
btn->setScale(ns);

if (!tapped) {
_animateButtonState(i, _selectedColor == i ? ACTIVE : INACTIVE);
continue;
}
if (input.justReleased()) {
_selectedColor = i;
_animateButtonState(i, ACTIVE);
} else {
_animateButtonState(i, PRESSED);
}
}
} else {
for (uint i = 0, j = (uint) _colors.size(); i < j; i++)
_animateButtonState(i, _selectedColor == i ? ACTIVE : INACTIVE);
}
}







Loading

0 comments on commit ad62bc0

Please sign in to comment.