Skip to content

Commit 9482cf6

Browse files
authored
Merge pull request #159 from scratchcpp/stage_pen
Refactor target models
2 parents c5ec812 + 5e5ec4f commit 9482cf6

15 files changed

+1272
-941
lines changed

src/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ qt_add_qml_module(scratchcpp-render
2424
projectloader.h
2525
projectscene.cpp
2626
projectscene.h
27+
targetmodel.cpp
28+
targetmodel.h
2729
stagemodel.cpp
2830
stagemodel.h
2931
spritemodel.cpp

src/ProjectPlayer.qml

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ ProjectScene {
173173
mouseArea: sceneMouseArea
174174
stageScale: root.stageScale
175175
onStageModelChanged: stageModel.renderedTarget = this
176+
Component.onCompleted: stageModel.penLayer = projectPenLayer
176177
}
177178

178179
Loader {

src/blocks/penblocks.cpp

+28-24
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ unsigned int PenBlocks::stamp(libscratchcpp::VirtualMachine *vm)
220220

221221
unsigned int PenBlocks::penDown(VirtualMachine *vm)
222222
{
223-
SpriteModel *model = getSpriteModel(vm);
223+
TargetModel *model = getTargetModel(vm);
224224

225225
if (model)
226226
model->setPenDown(true);
@@ -230,7 +230,7 @@ unsigned int PenBlocks::penDown(VirtualMachine *vm)
230230

231231
unsigned int PenBlocks::penUp(libscratchcpp::VirtualMachine *vm)
232232
{
233-
SpriteModel *model = getSpriteModel(vm);
233+
TargetModel *model = getTargetModel(vm);
234234

235235
if (model)
236236
model->setPenDown(false);
@@ -240,7 +240,7 @@ unsigned int PenBlocks::penUp(libscratchcpp::VirtualMachine *vm)
240240

241241
unsigned int PenBlocks::changePenSizeBy(libscratchcpp::VirtualMachine *vm)
242242
{
243-
SpriteModel *model = getSpriteModel(vm);
243+
TargetModel *model = getTargetModel(vm);
244244

245245
if (model)
246246
model->penAttributes().diameter = std::clamp(model->penAttributes().diameter + vm->getInput(0, 1)->toDouble(), PEN_SIZE_MIN, PEN_SIZE_MAX);
@@ -250,7 +250,7 @@ unsigned int PenBlocks::changePenSizeBy(libscratchcpp::VirtualMachine *vm)
250250

251251
unsigned int PenBlocks::setPenSizeTo(libscratchcpp::VirtualMachine *vm)
252252
{
253-
SpriteModel *model = getSpriteModel(vm);
253+
TargetModel *model = getTargetModel(vm);
254254

255255
if (model)
256256
model->penAttributes().diameter = std::clamp(vm->getInput(0, 1)->toDouble(), PEN_SIZE_MIN, PEN_SIZE_MAX);
@@ -260,7 +260,7 @@ unsigned int PenBlocks::setPenSizeTo(libscratchcpp::VirtualMachine *vm)
260260

261261
unsigned int PenBlocks::changePenShadeBy(libscratchcpp::VirtualMachine *vm)
262262
{
263-
SpriteModel *model = getSpriteModel(vm);
263+
TargetModel *model = getTargetModel(vm);
264264

265265
if (model) {
266266
PenState &penState = model->penState();
@@ -272,7 +272,7 @@ unsigned int PenBlocks::changePenShadeBy(libscratchcpp::VirtualMachine *vm)
272272

273273
unsigned int PenBlocks::setPenShadeToNumber(libscratchcpp::VirtualMachine *vm)
274274
{
275-
SpriteModel *model = getSpriteModel(vm);
275+
TargetModel *model = getTargetModel(vm);
276276

277277
if (model)
278278
setPenShade(vm->getInput(0, 1)->toInt(), model->penState());
@@ -282,7 +282,7 @@ unsigned int PenBlocks::setPenShadeToNumber(libscratchcpp::VirtualMachine *vm)
282282

283283
unsigned int PenBlocks::changePenHueBy(libscratchcpp::VirtualMachine *vm)
284284
{
285-
SpriteModel *model = getSpriteModel(vm);
285+
TargetModel *model = getTargetModel(vm);
286286

287287
if (model) {
288288
PenState &penState = model->penState();
@@ -296,7 +296,7 @@ unsigned int PenBlocks::changePenHueBy(libscratchcpp::VirtualMachine *vm)
296296

297297
unsigned int PenBlocks::setPenHueToNumber(libscratchcpp::VirtualMachine *vm)
298298
{
299-
SpriteModel *model = getSpriteModel(vm);
299+
TargetModel *model = getTargetModel(vm);
300300

301301
if (model) {
302302
PenState &penState = model->penState();
@@ -311,7 +311,7 @@ unsigned int PenBlocks::setPenHueToNumber(libscratchcpp::VirtualMachine *vm)
311311

312312
unsigned int PenBlocks::setPenColorToColor(libscratchcpp::VirtualMachine *vm)
313313
{
314-
SpriteModel *model = getSpriteModel(vm);
314+
TargetModel *model = getTargetModel(vm);
315315

316316
if (model) {
317317
const Value *value = vm->getInput(0, 1);
@@ -358,7 +358,7 @@ unsigned int PenBlocks::setPenColorToColor(libscratchcpp::VirtualMachine *vm)
358358

359359
unsigned int PenBlocks::changePenColorParamBy(VirtualMachine *vm)
360360
{
361-
SpriteModel *model = getSpriteModel(vm);
361+
TargetModel *model = getTargetModel(vm);
362362

363363
if (model) {
364364
const auto it = COLOR_PARAM_MAP.find(vm->getInput(0, 2)->toString());
@@ -374,7 +374,7 @@ unsigned int PenBlocks::changePenColorParamBy(VirtualMachine *vm)
374374

375375
unsigned int PenBlocks::changePenColorBy(VirtualMachine *vm)
376376
{
377-
SpriteModel *model = getSpriteModel(vm);
377+
TargetModel *model = getTargetModel(vm);
378378

379379
if (model)
380380
setOrChangeColorParam(ColorParam::COLOR, vm->getInput(0, 1)->toDouble(), model->penState(), true);
@@ -384,7 +384,7 @@ unsigned int PenBlocks::changePenColorBy(VirtualMachine *vm)
384384

385385
unsigned int PenBlocks::changePenSaturationBy(VirtualMachine *vm)
386386
{
387-
SpriteModel *model = getSpriteModel(vm);
387+
TargetModel *model = getTargetModel(vm);
388388

389389
if (model)
390390
setOrChangeColorParam(ColorParam::SATURATION, vm->getInput(0, 1)->toDouble(), model->penState(), true);
@@ -394,7 +394,7 @@ unsigned int PenBlocks::changePenSaturationBy(VirtualMachine *vm)
394394

395395
unsigned int PenBlocks::changePenBrightnessBy(VirtualMachine *vm)
396396
{
397-
SpriteModel *model = getSpriteModel(vm);
397+
TargetModel *model = getTargetModel(vm);
398398

399399
if (model)
400400
setOrChangeColorParam(ColorParam::BRIGHTNESS, vm->getInput(0, 1)->toDouble(), model->penState(), true);
@@ -404,7 +404,7 @@ unsigned int PenBlocks::changePenBrightnessBy(VirtualMachine *vm)
404404

405405
unsigned int PenBlocks::changePenTransparencyBy(VirtualMachine *vm)
406406
{
407-
SpriteModel *model = getSpriteModel(vm);
407+
TargetModel *model = getTargetModel(vm);
408408

409409
if (model)
410410
setOrChangeColorParam(ColorParam::TRANSPARENCY, vm->getInput(0, 1)->toDouble(), model->penState(), true);
@@ -414,7 +414,7 @@ unsigned int PenBlocks::changePenTransparencyBy(VirtualMachine *vm)
414414

415415
unsigned int PenBlocks::setPenColorParamTo(VirtualMachine *vm)
416416
{
417-
SpriteModel *model = getSpriteModel(vm);
417+
TargetModel *model = getTargetModel(vm);
418418

419419
if (model) {
420420
const auto it = COLOR_PARAM_MAP.find(vm->getInput(0, 2)->toString());
@@ -430,7 +430,7 @@ unsigned int PenBlocks::setPenColorParamTo(VirtualMachine *vm)
430430

431431
unsigned int PenBlocks::setPenColorTo(VirtualMachine *vm)
432432
{
433-
SpriteModel *model = getSpriteModel(vm);
433+
TargetModel *model = getTargetModel(vm);
434434

435435
if (model)
436436
setOrChangeColorParam(ColorParam::COLOR, vm->getInput(0, 1)->toDouble(), model->penState(), false);
@@ -440,7 +440,7 @@ unsigned int PenBlocks::setPenColorTo(VirtualMachine *vm)
440440

441441
unsigned int PenBlocks::setPenSaturationTo(VirtualMachine *vm)
442442
{
443-
SpriteModel *model = getSpriteModel(vm);
443+
TargetModel *model = getTargetModel(vm);
444444

445445
if (model)
446446
setOrChangeColorParam(ColorParam::SATURATION, vm->getInput(0, 1)->toDouble(), model->penState(), false);
@@ -450,7 +450,7 @@ unsigned int PenBlocks::setPenSaturationTo(VirtualMachine *vm)
450450

451451
unsigned int PenBlocks::setPenBrightnessTo(VirtualMachine *vm)
452452
{
453-
SpriteModel *model = getSpriteModel(vm);
453+
TargetModel *model = getTargetModel(vm);
454454

455455
if (model)
456456
setOrChangeColorParam(ColorParam::BRIGHTNESS, vm->getInput(0, 1)->toDouble(), model->penState(), false);
@@ -460,24 +460,28 @@ unsigned int PenBlocks::setPenBrightnessTo(VirtualMachine *vm)
460460

461461
unsigned int PenBlocks::setPenTransparencyTo(VirtualMachine *vm)
462462
{
463-
SpriteModel *model = getSpriteModel(vm);
463+
TargetModel *model = getTargetModel(vm);
464464

465465
if (model)
466466
setOrChangeColorParam(ColorParam::TRANSPARENCY, vm->getInput(0, 1)->toDouble(), model->penState(), false);
467467

468468
return 1;
469469
}
470470

471-
SpriteModel *PenBlocks::getSpriteModel(libscratchcpp::VirtualMachine *vm)
471+
TargetModel *PenBlocks::getTargetModel(libscratchcpp::VirtualMachine *vm)
472472
{
473473
Target *target = vm->target();
474474

475-
if (!target || target->isStage())
475+
if (!target)
476476
return nullptr;
477477

478-
Sprite *sprite = static_cast<Sprite *>(target);
479-
SpriteModel *model = static_cast<SpriteModel *>(sprite->getInterface());
480-
return model;
478+
if (target->isStage()) {
479+
Stage *stage = static_cast<Stage *>(target);
480+
return static_cast<StageModel *>(stage->getInterface());
481+
} else {
482+
Sprite *sprite = static_cast<Sprite *>(target);
483+
return static_cast<SpriteModel *>(sprite->getInterface());
484+
}
481485
}
482486

483487
void PenBlocks::setOrChangeColorParam(ColorParam param, double value, PenState &penState, bool change)

src/blocks/penblocks.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace scratchcpprender
99
{
1010

11-
class SpriteModel;
11+
class TargetModel;
1212
class PenState;
1313

1414
class PenBlocks : public libscratchcpp::IExtension
@@ -78,7 +78,7 @@ class PenBlocks : public libscratchcpp::IExtension
7878
TRANSPARENCY
7979
};
8080

81-
static SpriteModel *getSpriteModel(libscratchcpp::VirtualMachine *vm);
81+
static TargetModel *getTargetModel(libscratchcpp::VirtualMachine *vm);
8282
static void setOrChangeColorParam(ColorParam param, double value, PenState &penState, bool change);
8383
static void setPenShade(int shade, PenState &penState);
8484
static void legacyUpdatePenColor(PenState &penState);

0 commit comments

Comments
 (0)