File: Action.cpp

package info (click to toggle)
structure-synth 1.5.0-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,268 kB
  • ctags: 1,966
  • sloc: cpp: 10,209; python: 164; makefile: 71; sh: 15
file content (104 lines) | stat: -rw-r--r-- 2,415 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "Action.h"
#include "ExecutionStack.h"
#include "Builder.h"

#include "../../SyntopiaCore/Logging/Logging.h"

using namespace SyntopiaCore::Logging;

namespace StructureSynth {
	namespace Model {	
		void Action::apply(Builder* b, const Rule* callingRule, int ruleDepth) const {
			bool rememberPreviousMatrix = true; // at some point we might make this optional -> only needed for grid meshes...

			if (set != 0) {
				b->setCommand(set->key, set->value);
				return;
			}

			State s = b->getState();
			
			
			QList<int> counters;
			for (int i = 0; i < loops.size(); i++) counters.append(1);

			if (counters.size() == 0) {
				if (callingRule) {
					s.maxDepths[callingRule] = ruleDepth;
				}
				b->getNextStack().append(RuleState(rule->rule(), s));
				return;
			}

			bool done = false;
			while (!done) {

				// create state
				State s0 = s;
				if (rememberPreviousMatrix) {
					// Copy the old matrix...
					s0.setPreviousState(s.matrix, s.hsv, s.alpha);
				}
				for (int i = 0; i < counters.size(); i++) {
					for (int j = 0; j < counters[i]; j++) {
						s0 = loops[i].transformation.apply(s0, b->getColorPool());
					}
				}
				if (callingRule) {
					s0.maxDepths[callingRule] = ruleDepth;
				}
				b->getNextStack().append(RuleState(rule->rule(), s0));

				// increase lowest counter...
				counters[0]++;
				for (int i = 0; i < counters.size(); i++) {
					if (counters[i] > loops[i].repetitions) {
						if (i == counters.size()-1) {
							done = true;
						} else {
							counters[i] = 1;
							counters[i+1]++;
						}
					}
				}

			}
		}

		Action::Action(QString key, QString value) {
			set = new SetAction();
			set->key = key;
			set->value = value;
			rule = 0;
		}

		Action::~Action() {
			// TODO: Handle leaks (Actions are treated as value types, and hence rule,set ptrs are duped)
			//delete(rule);
			//delete(set);
		}


		void Action::addTransformationLoop(TransformationLoop tl) { 
			loops.append(tl); 
		}

		void Action::setRule(QString ruleName) {
			rule = new RuleRef(ruleName);
			set = 0;
		}

		Action::Action(Transformation t, QString ruleName) {
			loops.append(TransformationLoop(1, t));
			rule = new RuleRef(ruleName);
			set = 0;
		}

		Action::Action(QString ruleName) {
			rule = new RuleRef(ruleName);
			set = 0;
		}

	}
}