id
stringlengths
36
36
text
stringlengths
1
1.25M
4ab527f9-6781-4d6e-8da9-f36448e47114
static boolean isAssociativeSequence(String str) { if (null == str || str.isEmpty()) { return false; } char firstChar = str.charAt(0); if ('+' != firstChar && '*' != firstChar) { return false; } for (Character character : str.toCharArray()) { if (firstChar != character) { return false; } } return true; }
29fd3fd7-fc55-4ac3-8205-c1bfcfd5618d
static String[] splitAndGetAsArray(String str) { if (null == str || str.isEmpty()) { return new String[0]; } String[] res = new String[str.length()]; int index = 0; for (Character character : str.toCharArray()) { res[index++] = Character.toString(character); } return res; }
d868f82b-71e1-4bcc-bce2-5456fd33709a
static String[] getOperands() { Scanner scanner = new Scanner(System.in); int size; System.out.println("Enter the number of elements!"); size = scanner.nextInt(); if (0 >= size) { throw new RuntimeException("invalid size: " + size); } int index = 0; String operands[] = new String[size]; while (index < size) { String operand = scanner.next(); if (operand.length() != 1) { throw new RuntimeException("operand should be a single digit!"); } for (Character ch : operand.toCharArray()) { if (!Character.isDigit(ch)) { throw new RuntimeException("Illegal code point: " + ch); } } operands[index++] = operand; } return operands; }
3618326c-db1a-4851-af73-ded1544b7f03
public static void main(String[] args) { EvalSumAccumulator accumulator = new EvalSumAccumulator(); String[] operands = getOperands(); List<String> assocs = new AssociationGenrator(operands).getAssociations(); List<String> opSeqs = new OperatorSequenceGenrator(operands.length - 1).getSequence(); int count = 0; for (String opSeq : opSeqs) { String[] operators = splitAndGetAsArray(opSeq); if (isAssociativeSequence(opSeq)) { accumulator.accumulate(String.format(assocs.get(0), (Object[]) operators)); count++; continue; } for (String assoc : assocs) { accumulator.accumulate(String.format(assoc, (Object[]) operators)); count++; } } System.out.println(accumulator.getFirstMinimumImpossibleNumber()); }
299797ff-a226-4aa6-b31a-1f3faf4db5a3
public int eval(String infix) { return evaluatePostfix(convertToPostfix(infix)); }
d064b025-2120-49e7-96ed-d1ef8d710d9b
private String convertToPostfix(String infixExpression) { char[] tokens = infixExpression.toCharArray(); Stack<Character> stack = new Stack<>(); StringBuilder postfixBuilder = new StringBuilder(infixExpression.length()); for (char c : tokens) { if (isOperator(c)) { while (!stack.isEmpty() && stack.peek() != '(') { if (operatorGreaterOrEqual(stack.peek(), c)) { postfixBuilder.append(stack.pop()); } else { break; } } stack.push(c); } else if (c == '(') { stack.push(c); } else if (c == ')') { while (!stack.isEmpty() && stack.peek() != '(') { postfixBuilder.append(stack.pop()); } if (!stack.isEmpty()) { stack.pop(); } } else if (isOperand(c)) { postfixBuilder.append(c); } } while (!stack.empty()) { postfixBuilder.append(stack.pop()); } return postfixBuilder.toString(); }
fc53da0c-4fcd-420b-a076-4cd4b4b7ee92
private int evaluatePostfix(String postfixExpr) { char[] chars = postfixExpr.toCharArray(); Stack<Integer> stack = new Stack<>(); for (char operator : chars) { if (isOperand(operator)) { stack.push(operator - '0'); } else if (isOperator(operator)) { int operand1 = stack.pop(); int operand2 = stack.pop(); int result; switch (operator) { case '*': result = operand1 * operand2; stack.push(result); break; case '/': result = operand2 / operand1; stack.push(result); break; case '+': result = operand1 + operand2; stack.push(result); break; case '-': result = operand2 - operand1; stack.push(result); break; default: throw new IllegalArgumentException("Illegal operation: " + operator); } } } return stack.pop(); }
f5690ee2-3391-4af0-b2bb-a9fe773ee49c
private int getPrecedence(char operator) { int ret = 0; if (operator == '-' || operator == '+') { ret = 1; } else if (operator == '*' || operator == '/') { ret = 2; } return ret; }
b6c20cef-2f3b-4918-95db-e45814bc7466
private boolean operatorGreaterOrEqual(char op1, char op2) { return getPrecedence(op1) >= getPrecedence(op2); }
81d85392-8cbf-47e8-bca6-4451a142133c
private boolean isOperator(char val) { return possibleOperators.indexOf(val) >= 0; }
77d45a14-c99d-4939-9ecd-8b8f038586b4
private boolean isOperand(char val) { return possibleOperands.indexOf(val) >= 0; }
8e3abcd4-554d-422a-8a9d-1407f53a4daf
public AppTest( String testName ) { super( testName ); }
975553f8-7163-4398-ba5f-bbce547ba3d4
public static Test suite() { return new TestSuite( AppTest.class ); }
2703c64c-a2a0-4b4b-8750-90ca24fcb279
public void testApp() { assertTrue( true ); }
173f5ecc-44e3-4035-8989-6f16a2fb1d48
T apply(T op1, T op2);
7dbdeef9-d856-456e-9dd9-4007b1918f47
default int getPrecedence() { return 5; }
6e36efb9-822e-49c6-97a6-b6ba005f4ec7
default T compose(final ComposableOperation<T> nextOperation, final T accOp1, final T accOp2, final T bridgeOp) { return getPrecedence() < nextOperation.getPrecedence() ? apply(nextOperation.apply(accOp1, accOp2), bridgeOp) : nextOperation.apply(apply(accOp1, accOp2), bridgeOp); }
2a5277e5-2db4-430c-bfbc-2145155d7527
public static <T> T doOperation(final Operation<T> operation, final T op1, final T op2) { return operation.apply(op1, op2); }
d4993088-abf0-4dd3-8135-d012632676cf
static Operation<Integer> getIntDiffOperation() { return ((op1, op2) -> op1 - op2); }
dfb59816-28ff-40a9-ab41-58c137e99578
public static int multiply(final int op1, final int op2) { return op1 * op2; }
3f46be23-fc0c-4b4b-b5dd-606db82219f0
int mod(final int op1, final int op2) { return op1 % op2; }
40f46187-335d-4499-a0bb-3322b291df25
public static void main(final String... args) { final Integer operand1 = 1, operand2 = 2, operand3 = 3; final Integer sum = LambdaFunction.<Integer>doOperation((op1, op2) -> op1 + op2, operand1, operand2); final Integer diff = LambdaFunction.doOperation(getIntDiffOperation(), operand1, operand2); final Integer product = LambdaFunction.doOperation(LambdaFunction::multiply, operand1, operand2); final Integer mod = LambdaFunction.doOperation(new LambdaFunction()::mod, operand1, operand2); // 1 + 2 * 3 final ComposableOperation<Integer> additionOperation = (op1, op2) -> op1 + op2; final ComposableOperation<Integer> productOperation = new ComposableOperation<Integer>() { @Override public Integer apply(final Integer op1, final Integer op2) { return op1 * op2; } @Override public int getPrecedence() { return 6; } }; final Integer exprVal = additionOperation.compose(productOperation, operand1, operand2, operand3); final Integer exprVal2 = productOperation.compose(additionOperation, operand1, operand2, operand3); System.out.println(String.format("OP1: %d OP2: %d OP3: %d %n SUM: %d %n DIFF: %d %n " + "PRODUCT: %d %n MOD: %d %n COMPOSE(OP1 + OP2 * OP3) 1: %d %n COMPOSE(OP1 + OP2 * OP3) 2: %d %n", operand1, operand2, operand3, sum, diff, product, mod, exprVal, exprVal2)); }
b8b2f8bb-2fc5-4427-85c0-ebb4f221bd63
@Override public Integer apply(final Integer op1, final Integer op2) { return op1 * op2; }
05d106cc-00ef-4923-af5f-1c507f3dba9b
@Override public int getPrecedence() { return 6; }
583b8ceb-59fc-4d31-886d-b5a4fcbaf7f9
public Node(T data) { this.data = data; }
33aaa105-b5cd-4233-8c04-1713d87e7ff0
public Stream<Node<T>> stream() { return StreamSupport.stream(new Supplier<Spliterator<Node<T>>>() { @Override public Spliterator<Node<T>> get() { return new Spliterator<Node<T>>() { @Override public boolean tryAdvance(Consumer<? super Node<T>> action) { action.accept(Node.this); return false; } @Override public Spliterator<Node<T>> trySplit() { return null; } @Override public long estimateSize() { return 0; } @Override public int characteristics() { return Spliterator.ORDERED | Spliterator.IMMUTABLE; } }; } }, Spliterator.ORDERED | Spliterator.IMMUTABLE, false); }
612b3269-abf9-4481-b919-7eef25e623b7
@Override public Spliterator<Node<T>> get() { return new Spliterator<Node<T>>() { @Override public boolean tryAdvance(Consumer<? super Node<T>> action) { action.accept(Node.this); return false; } @Override public Spliterator<Node<T>> trySplit() { return null; } @Override public long estimateSize() { return 0; } @Override public int characteristics() { return Spliterator.ORDERED | Spliterator.IMMUTABLE; } }; }
c042b0df-ea3a-4b15-9dc0-2c676ae52f50
@Override public boolean tryAdvance(Consumer<? super Node<T>> action) { action.accept(Node.this); return false; }
6d57f151-1886-46dd-88b1-cae42774bb1f
@Override public Spliterator<Node<T>> trySplit() { return null; }
d43fb265-40dd-41b4-b566-c6a133b3c1df
@Override public long estimateSize() { return 0; }
b630ddfc-6128-4c44-8255-b0b7362bfd32
@Override public int characteristics() { return Spliterator.ORDERED | Spliterator.IMMUTABLE; }
f83bd90b-7503-4b51-b3ef-b88c0e4d0bf8
public static void main(String[] args) { final Consumer<Node<Integer>> nodeSumConsumer = (node) -> { }; final Node<Integer> root = new Node<>(2); root.leftTree = new Node<>(1); root.rightTree = new Node<>(3); root.rightTree.rightTree = new Node<>(4); root.stream().forEach(nodeSumConsumer); }
1585a886-cfdc-40be-b854-119daf9bbe66
public InfiniteStream(final Supplier<T> supplier) { this.supplier = supplier; }
5ad87019-d8da-491f-9e9d-7447e2ea71af
@Override public boolean tryAdvance(Consumer<? super T> action) { action.accept(supplier.get()); return true; }
e2c4e7a6-2444-4c72-a8e4-69050342f346
@Override public Spliterator<T> trySplit() { return null; }
a621b3f9-2e7e-43e1-b719-6f04c2c7e0ba
@Override public long estimateSize() { return -1; }
abb0b242-6305-4f9f-a445-69ced3c1a646
@Override public int characteristics() { return Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.IMMUTABLE; }
3163a8e8-6e87-4f1e-bda3-643b39c37862
public static void main(String[] args) { final int sumOf100RandomNumers = Util.infiniteRandomPositiveIntegers(1000).limit(100).reduce(0, (acc, x) -> acc + x); System.out.println("SUM(100 random integers): " + sumOf100RandomNumers); final Supplier<String> bigStringSupplier = () -> { try { return new String(Files.readAllBytes(Paths.get("/Users/shasrp/Code/Personal/Github/my-experiments/java/java8/java8-demo/src/main/resources/name.txt"))); } catch (IOException e) { return ""; } }; final String threeConcatOfnameFile = StreamSupport.stream(new InfiniteStream<String>(bigStringSupplier), false). limit(3).reduce("", (acc, x) -> acc + x); System.out.println("CONCAT(3 names 3 times): " + threeConcatOfnameFile); //Map and Collectors examples System.out.println("SUM of first 10 natural numbers: " + Util.naturalNumbers().limit(10).reduce(0, (acc, next) -> acc + next)); System.out.println("product of squares of first 5 natural numbers: " + Util.naturalNumbers().limit(5). map(((Integer nat) -> LambdaFunction.<Integer>doOperation(LambdaFunction::multiply, nat, nat))). reduce(1, LambdaFunction::multiply)); System.out.println(" EVEN NAT < 100: " + Util.naturalNumbers().limit(100).filter((nat) -> 0 == (nat % 2)).collect(Collectors.toList())); System.out.println("UNIQUE NAT in random ints: " + Util.infiniteRandomPositiveIntegers(100).limit(101).collect(Collectors.toSet())); }
ca9e55d5-57a0-4313-9139-e5454aed5e83
public static Stream<Integer> naturalNumbers() { return StreamSupport.stream(new InfiniteStream<Integer>(new Supplier<Integer>() { int nextNaturalNumber = 0; @Override public Integer get() { return ++nextNaturalNumber; } }), false); }
f2fff72c-d0e8-457a-a28d-65a67c0cb5af
@Override public Integer get() { return ++nextNaturalNumber; }
a7ad3b3b-ebc6-482e-b8b8-7e35d5b178db
public static Stream<Integer> infiniteRandomPositiveIntegers(final int maxInt) { return Stream.generate(() -> new Random().nextInt(maxInt)); }
0bf0ea45-255d-46b0-a11d-3347e6b58002
public TextArea(ManagedPApplet i_Parent, int width, int height, Point i_TopLeft) { super(i_Parent); this.m_width = width; this.m_height = height; this.m_TopLeft = i_TopLeft; setVisible(false); setEnabled(false); }
0910cd5d-41d4-4337-9f60-f41522690e25
@Override public void update(long ellapsedTime) { this.m_DisplayTimeLeft -= ellapsedTime; // System.out.println("Display time left " + this.m_DisplayTimeLeft); // System.out.println("fade in time? " + (this.m_DisplayTimeLeft > this.m_TotalDisplayTime - this.m_introTime) ); // fade out if (this.m_DisplayTimeLeft < this.m_introTime) { float t = (float) ellapsedTime; this.m_Alpha -= (t / this.m_introTime) * 255; } // fade in if (this.m_DisplayTimeLeft >= this.m_TotalDisplayTime - this.m_introTime) { float t = (float)ellapsedTime; this.m_Alpha += (t / this.m_introTime) * 255; } if (this.m_DisplayTimeLeft < 0) { this.setVisible(false); this.setEnabled(false); } }
fdc53c90-d9b5-4c32-9479-6737e2750852
@Override public void draw(long ellapsedTime) { this.m_Parent.rectMode(this.m_Parent.CORNER); this.m_Parent.fill(255, this.m_Alpha); this.m_Parent.rect(m_TopLeft.x, m_TopLeft.y, this.m_width, this.m_height, 7); this.m_Parent.noStroke(); this.m_Parent.fill(0); this.m_Parent.textAlign(this.m_Parent.TOP, this.m_Parent.LEFT); this.m_Parent.text(this.m_Title, this.m_TopLeft.x + 30, this.m_TopLeft.y + 30); if (this.m_Messages != null) { for (int i = 0; i < this.m_Messages.length; i++) { this.m_Parent.text(this.m_Messages[i], this.m_TopLeft.x + 30, this.m_TopLeft.y + 30 * (i + 2)); } } }
de0bbeac-a949-46e5-812a-92b85fdcfb11
public void displayMessage(String i_Title, String[] i_Messages, int i_Time) { this.m_DisplayTimeLeft = i_Time + this.m_introTime * 2; this.m_TotalDisplayTime = i_Time + this.m_introTime * 2; this.m_Title = i_Title; this.m_Messages = i_Messages; if (i_Messages != null) { System.out.println("new messages " + i_Messages[0]); } this.setVisible(true); this.setEnabled(true); }
d5411a0b-9511-455d-95f7-a945a71fda41
public void setup() { super.setup(); // set size and framerate size (displayWidth, displayHeight); // frame.setResizable(true); frameRate(FRAMERATE); // load images, fonts, sounds... this.loadContent(); // set graphics options ellipseMode(CENTER); this.setBackgroundColor(BACKGROUND_COLOR); // initialize Sound Elements: m_Sounds = new Sound [NUMBER_OF_SOUNDS]; // m_Sounds[0] = new Sound (new PVector(this.random(0, WIDTH),this.random(-HEIGHT * 1, HEIGHT * 2)), // random(30, 100), this, 0); for (int i = 0; i < this.m_Sounds.length; i++) { // System.out.println("New sound"); // generate new random sound // TODO: calculate sound parameters according to real info PVector origin = new PVector(this.random(-displayWidth, displayWidth * 2),this.random(-displayHeight, displayHeight * 2)); float radius = this.random(30,100); // boolean validLocation = true; // int tryNum = 1; // do // { // origin = new PVector(this.random(-WIDTH * 1, WIDTH * 2),this.random(-HEIGHT * 1, HEIGHT * 2)); // // radius = random(30,100); // // // for (Sound sound : this.m_Sounds) // { // if (sound != null) // { // validLocation &= !sound.hasIntersection(origin, radius); // // if (!validLocation) // { // break; // } // } // } // tryNum++; // } while (!validLocation && tryNum < 100); m_Sounds [i] = new Sound (origin, radius, this, i); } // Initialize services this.m_AutoPlayingManager = new AutoPlayingManager(this); this.m_SoundManager = new SoundManager(this); // this.m_TextArea = new TextArea(this, 250, 100, new Point(WIDTH - 250, HEIGHT - 100)); }
376af244-8fb2-4411-a759-0e053eddfc0b
private void loadContent() { // load images: m_LegendImage = loadImage (LEGEND_IMAGE); m_TitleImage = loadImage(TITLE_IMAGE); m_SearchImage = loadImage(SEARCH_IMAGE); // load font m_Font = createFont("Georgia", 18); textFont(m_Font); }
2b32025e-f612-418a-aeaf-03741d0caa8a
@Override public void pre() { // if demo mode is not enabled, aggregate idleTime if (!this.m_AutoPlayingManager.Enabled()) { this.m_IdleTime += (millis() - this.m_PrevUpdateTime); // Activate demo mode if max idle time reached if (this.m_IdleTime >= AutoPlayingManager.IDLE_USER_TIME) { this.m_AutoPlayingManager.Activate(); } } super.pre(); }
6beec7c2-a7ad-4090-b9d5-445970706d73
public void draw() { super.draw(); // draw title images // image(m_LegendImage, 10,530); image(m_TitleImage, 10, 20); image(m_SearchImage, -25, 50); // draw search: text(m_UserInput, 30, 85); }
3e9bf763-f832-4127-b884-1a8fbcc2adad
@Override protected void drawBackground() { super.drawBackground(); if (DRAW_STROKES) { for (int row=0; row<5000; row=row+20) { stroke(BACKGROUND_STROKE_COLOR.getRed(), BACKGROUND_STROKE_COLOR.getGreen(), BACKGROUND_STROKE_COLOR.getBlue()); strokeWeight(1); line(0,row,row,0); } } }
7e13ff92-f209-432c-b430-6820562a9c8d
@Override public void mouseClicked (MouseEvent e) { super.mouseClicked(e); this.m_AutoPlayingManager.setEnabled(false); }
4e43cffe-13d5-4fe0-9e9c-b1c6db66ba4c
@Override public void mouseWheelMoved(MouseWheelEvent e) { // TODO Auto-generated method stub super.mouseWheelMoved(e); }
3aabebee-666f-435f-b214-1ddeae139d54
protected void handleMouseEvent(MouseEvent e) { super.handleMouseEvent(e); // reset idle time if mouse event happened this.m_IdleTime = 0; // disable demo mode if needed if (this.m_AutoPlayingManager.Enabled()) { this.m_AutoPlayingManager.Deactivate(); } }
d39e3143-17dd-4fd1-b292-fb5317d564b3
public Sound[] getSounds() { return this.m_Sounds; }
879029e6-284c-4d7f-8781-d0262d84ffff
static public void main(String[] passedArgs) { String[] appletArgs = new String[] { "--full-screen", "--bgcolor=#666666", "--stop-color=#cccccc", "Main" }; // String[] appletArgs = new String[] { "Main" }; if (passedArgs != null) { PApplet.main(concat(appletArgs, passedArgs)); } else { PApplet.main(appletArgs); } }
03ee7fab-a043-4675-8527-187ad4984fc3
public Pulse(PVector i_Origin, float i_Diameter, Color i_Color, long i_LifeTime, Main i_Parent) { super(i_Parent); this.m_Origin = i_Origin; this.m_Diameter = i_Diameter; this.m_OriginalDiameter = i_Diameter; this.m_Color = i_Color; this.m_ParentApp = i_Parent; // TODO: support lifetime in calculations of growthRate and fadePerSecond this.m_LifeTime = i_LifeTime; }
524884bb-1663-483b-aca4-55d72fb0482b
@Override public void update(long elapsedTime) { this.m_LifeTime -= elapsedTime; // If circle is not too old and still visible (alpha > 0) if (this.m_LifeTime > 0 && this.m_Alpha > 0) { // grow and fade the circle this.grow(elapsedTime); this.fade(elapsedTime); } else { // get rid of circle this.die(); } }
72f42cb5-1578-4e02-a579-c917f7cc88b2
@Override public void draw(long elapsedTime) { this.m_ParentApp.stroke(this.m_Color.getRGB(), this.m_Alpha); this.m_ParentApp.noFill(); this.m_ParentApp.ellipse(this.m_Origin.x, this.m_Origin.y, this.m_Diameter, this.m_Diameter); }
b8db4331-fb3d-482b-b41a-935ae398a8f9
public boolean isDead() { return this.m_dead; }
d0fb713d-a07b-470a-bbde-ea9d42cf9399
private void fade(float elapsedTime) { this.m_Alpha -= (this.m_fadePerSecond * elapsedTime / 1000f); // ensure alpha is not negative if (this.m_Alpha <= 0) { this.m_Alpha = 0; } }
9f1f473d-38f5-4ee8-9243-877af20f65d4
private void grow(float elapsedTime) { this.m_Diameter += this.m_growthPerSecond * elapsedTime / 1000f; this.m_growthPerSecond += this.m_acceleration * elapsedTime / 1000f; }
85ccf852-7876-4472-9c28-6cc259faba40
public AutoPlayingManager(Main i_Parent) { super(i_Parent); this.m_MainParent = i_Parent; // Set initial wait time from starting Demo mode // to playing first sound generateNewStopTime(); this.m_delayTime = 2000; }
6d749b75-2fc4-4f50-a03a-00dd58f2d6ca
private void generateNewStopTime() { this.m_TimeToStop = (int) this.m_MainParent.random(MINIMUM_TIME_BETWEEN_ACTIVATIONS, MAXIMUM_TIME_BETWEEN_ACTIVATIONS); }
ef844c55-75f5-4f72-89c0-adee05f9d2a8
private void generateNewPlayTime() { this.m_TimeToPlay = (int) this.m_MainParent.random(MINIMUM_ACTIVATION_TIME, MAXIMUM_ACTIVATION_TIME); }
64953a97-2487-43a2-8dbd-1c3d4a25de99
@Override public void update(long ellapsedTime) { // If a sound is playing if (isPlaying()) { // keep track of playing time this.m_PlayingTime += ellapsedTime; // check if need to stop playing: if (this.m_PlayingTime > this.m_TimeToPlay) { stopPlaying(); } // If still supposed to play and no sound is playing else if (!this.m_MainParent.getSounds()[this.m_ActiveSoundIndex].isPlayingSound()) { this.m_delayTime -= ellapsedTime; // check if enough time has passed (delayTime) if (this.m_delayTime < 0) { // trigger next sound and reset delayTime this.m_MainParent.getSounds()[this.m_ActiveSoundIndex].activate(false); this.m_delayTime = 2000; } } } else { // keep tracked of stopped time this.m_StoppedTime += ellapsedTime; // Check if new sound needs to be selected: if (this.m_StoppedTime > this.m_TimeToStop) { selectNextSound(); } } }
f54ad4c8-252b-4479-91eb-c74f5c637ef9
private void selectNextSound() { // Move to next sound in Sounds - cycle back if needed this.m_ActiveSoundIndex++; this.m_ActiveSoundIndex %= this.m_MainParent.getSounds().length; // activate the sound and generate random playing time within defined values Sound selectedSound = this.m_MainParent.getSounds()[this.m_ActiveSoundIndex]; selectedSound.activate(false); generateNewPlayTime(); // set playing flag and reset playing time counter this.m_PlayingTime = 0; this.m_Playing = true; }
337cde45-c4fd-45eb-8adf-5e3a9a5ea372
private void stopPlaying() { generateNewStopTime(); this.m_StoppedTime = 0; this.m_Playing = false; }
4f6cbe40-97aa-4bff-8776-77775989ebc0
public boolean isPlaying() { return this.m_Playing; }
94cf8f49-a4f6-4b7d-9b7f-2532735de93f
@Override public void draw(long ellapsedTime) { // Draw Demo Mode message - DEBUG ONLY this.m_MainParent.fill(155); this.m_MainParent.text("Demo Mode", this.m_MainParent.width / 2, 10); }
648a84de-6053-4cea-88e1-eb65c5aa6651
public void Deactivate() { this.setEnabled(false); this.setVisible(false); }
0d5acab9-d8bb-4483-aa3b-79199282491e
public void Activate() { this.setEnabled(true); this.setVisible(true); }
27a907b5-0da1-4ee4-9699-c562ae70c6b2
public void addComponent(Component i_Component) { this.m_ComponentsToAdd.add(i_Component); }
f08b607e-b583-4574-b1b8-cc7dc33e9c18
public void removeComponent(Component i_Component) { this.m_ComponentsToRemove.add(i_Component); }
e7cd5ff0-ba05-4de0-96d6-e1e4469be22b
@Override public void setup() { super.setup(); // register for pre event to manage elapsed time and call update() // on all updateables automatically registerMethod("pre", this); }
5f9d88b4-d699-47b8-9604-103f9cd9bbe3
public void pre() { // keep track of time int currentTime = millis(); int elapsedTime = currentTime - this.m_PrevUpdateTime; // add and remove components as needed // separated from addComponent to allow subclasses of Component // to call addComponent at any time without // risking ConcurrentModificationException addNewComponents(); removeComponents(); // update camera settings if needed to animate: if (this.m_TimeLeftForCameraAnimation > 0) { // this.m_Zoom += ((this.m_TargetZoom - this.m_Zoom) / this.m_TimeLeftForCameraAnimation) * elapsedTime; // PVector step = PVector.sub(this.m_TargetOffset, this.m_WorldOrigin); // step.div(this.m_TimeLeftForCameraAnimation); // step.mult(elapsedTime); // this.setOffset(PVector.add(this.m_WorldOrigin, step)); this.m_Zoom += this.m_ZoomStep * elapsedTime; this.m_WorldOrigin.add(PVector.mult(this.m_OffsetStep, elapsedTime)); this.m_TimeLeftForCameraAnimation -= elapsedTime; } pushMatrix(); // center on zoom target this.translate(0, 0); // this.translate(this.width / 2, this.height /2); // this.translate(this.m_CenterForZoom.x, this.m_CenterForZoom.y); // zoom in / out as needed this.scale(this.m_Zoom); // move back // this.translate(-this.m_CenterForZoom.x / this.m_Zoom, -this.m_CenterForZoom.y / this.m_Zoom); // translate center (accounting for zoom) this.translate(this.m_WorldOrigin.x / this.m_Zoom, this.m_WorldOrigin.y / this.m_Zoom); // update all components if they are enabled for (Component component : this.m_Components) { if (component.Enabled()) { component.update(elapsedTime); } } popMatrix(); this.m_PrevUpdateTime = currentTime; }
eb2710bd-c4c3-4887-952f-804a529881c6
@Override public void draw() { // keep track of time int currentTime = millis(); int elapsedTime = currentTime - this.m_PrevDrawTime; drawBackground(); // save world coordinates pushMatrix(); // center on zoom target this.translate(0, 0); // this.translate(this.width / 2, this.height /2); // this.translate(this.m_CenterForZoom.x, this.m_CenterForZoom.y); // zoom in / out as needed this.scale(this.m_Zoom); // move back // this.translate(-this.m_CenterForZoom.x / this.m_Zoom, -this.m_CenterForZoom.y / this.m_Zoom); // translate center (accounting for zoom) this.translate(this.m_WorldOrigin.x / this.m_Zoom, this.m_WorldOrigin.y / this.m_Zoom); // draw all components if visible for (Component component : this.m_Components) { if (component.Visible()) { component.draw(elapsedTime); } } // return to world coordinates this.popMatrix(); if (this.m_ZoomEnabled) { drawZoomBar(); } if (DEBUG) { // draw framerate fill(255); textAlign(LEFT, TOP); text(frameRate, 10, 10); // draw world transformation and mouse info textAlign(LEFT, BOTTOM); text("Mouse world coordinates " + getWorldMouse(), 10, this.height - 10); text("Mouse actual coordinates" + new PVector(mouseX, mouseY), 10, this.height - 40); text("World Origin is at " + this.m_WorldOrigin, 10, this.height - 70); text("Screen center is at " + this.getWorldCenter(), 10, this.height - 100); text("Calculated world origin is at " + this.calcWorldOffset(getWorldCenter(), this.m_Zoom), 10, this.height - 130); // System.out.println("Mouse world coordinates" + result); // System.out.println("Actual mouse is on " + mouseX + " " + mouseY); } this.m_PrevDrawTime = currentTime; }
a124ce34-deca-452d-94c7-347face1aa0e
private void drawZoomBar() { // set graphics settings for vertical zoom bar this.noFill(); this.stroke(this.m_ZoomBarColor.getRGB()); this.strokeWeight(5); // vertical part of zoom bar this.line(this.width - 30, this.m_ZoomBarBottom - this.m_ZoomBarHeight, this.width - 30, this.m_ZoomBarBottom); // TODO: draw scale on it? // draw current zoom "location" this.strokeWeight(3); float zoomPercent = (this.m_Zoom - MIN_ZOOM) / (MAX_ZOOM - MIN_ZOOM); float yLocation = this.m_ZoomBarBottom - (zoomPercent * this.m_ZoomBarHeight); this.line(this.width - 30 - 10, yLocation, this.width - 30 + 10, yLocation); }
719a183a-3325-4807-b6cd-0eecb0f27378
protected void drawBackground() { clear(); this.background(this.m_BackgroundColor.getRGB()); }
44b8aaf9-2adb-41e2-9d5b-6f5aacd70046
public void setBackgroundColor(Color i_Color) { this.m_BackgroundColor = new Color(i_Color.getColorSpace(), i_Color.getRGBColorComponents(null), i_Color.getAlpha() / 255f); }
0e262bad-cbbf-4cd6-9bdd-b1aaa55e7a04
@Override public void handleDeath(Object object) { this.removeComponent((Component)object); }
a07dff89-693b-4528-ade3-7032a1ef9dea
private void removeComponents() { // remove all components set to be removed in ComponentsToRemove for (Component component : this.m_ComponentsToRemove) { this.m_Components.remove(component); } // clear componentsToRemove list this.m_ComponentsToRemove.clear(); }
7f5b1524-44c2-419a-b6db-16e25e7672b2
private void addNewComponents() { // Add all components set to be added in ComponentsToAdd for (Component component : this.m_ComponentsToAdd) { this.m_Components.add(component); } // clear componentsToAdd list this.m_ComponentsToAdd.clear(); }
3ccdcba3-38a3-4d90-9dd3-fc975d48c3ea
@Override public void mouseWheelMoved(MouseWheelEvent e) { super.mouseWheelMoved(e); if (this.m_ZoomEnabled) { zoom(e.getWheelRotation() < 0); } }
4329cff7-5ae9-4632-aa07-850b25e7e665
@Override public void mouseClicked(MouseEvent event) { super.mouseClicked(event); if (mouseButton == RIGHT) { this.toggleZoom(); } else if (mouseButton == CENTER) { this.resetView(); } else { this.getWorldMouse(); } }
392b0c4e-dd61-40fe-a625-47f2121feafb
public void zoom(boolean i_zoomIn) { float zoomStep = 0.1f; if (!i_zoomIn) { zoomStep *= -1; } this.m_Zoom *= (1 + zoomStep); this.m_Zoom = constrain(m_Zoom, MIN_ZOOM, MAX_ZOOM); if (DEBUG) { System.out.println("new zoom is " + this.m_Zoom); System.out.println("zoom-in?" + i_zoomIn); } this.m_CenterForZoom = new PVector(mouseX / this.m_Zoom, mouseY / this.m_Zoom); }
24e8a897-0e41-4ecf-873f-3a126d5e18ac
@Override public void mouseDragged(MouseEvent event) { // TODO Auto-generated method stub super.mouseDragged(event); if (this.m_ZoomEnabled) { PVector translation = new PVector(mouseX - this.m_PrevMouseX, mouseY - this.m_PrevMouseY); if (DEBUG) { System.out.println("MouseX is " + mouseX); System.out.println("translation is " + translation); } this.m_WorldOrigin.add(translation); } this.m_PrevMouseX = mouseX; this.m_PrevMouseY = mouseY; }
dcfe774e-ed2a-45c9-bfbb-f031b3dc8f8d
@Override public void mouseMoved(MouseEvent event) { super.mouseMoved(event); // keep track of mouse location this.m_PrevMouseX = mouseX; this.m_PrevMouseY = mouseY; }
77bf6143-4edb-4b7e-a72f-65e3b0b20713
public void enableZoom() { this.m_ZoomEnabled = true; if (DEBUG) { System.out.println("Zoom enabled"); } }
9cebc1f9-d17a-4385-a7b1-7ef86ef2bd96
public void disableZoom() { this.m_ZoomEnabled = false; if (DEBUG) { System.out.println("Zoom disabled"); } }
251a929b-5fb4-4cda-90ba-271b128ec7a8
public void toggleZoom() { if (this.m_ZoomEnabled) { this.disableZoom(); } else { this.enableZoom(); } }
684da73f-9e37-4734-850a-2490651397e7
public void resetView() { this.m_Zoom = 1.0f; this.m_WorldOrigin = new PVector(0, 0); if (DEBUG) { System.out.println("View reset"); } }
0c13946d-7f2c-4dd7-86bb-8d55384da326
public PVector getWorldMouse() { PVector result = new PVector((mouseX - this.m_WorldOrigin.x) / this.m_Zoom, (mouseY - this.m_WorldOrigin.y) / this.m_Zoom); // if (DEBUG) // { // System.out.println("Mouse world coordinates" + result); // System.out.println("Actual mouse is on " + mouseX + " " + mouseY); // } return result; }
ef423877-0e2f-4dce-b897-5fdfff0f09f4
public PVector getWorldCenter() { PVector result = new PVector(); result.sub(this.m_WorldOrigin); result.add(new PVector((this.width / 2) / this.m_Zoom, (this.height /2) / this.m_Zoom)); return result; }
7f10748a-284c-4713-b25c-1050158f030b
public PVector calcWorldOffset(PVector i_Center, float i_ZoomFactor) { PVector result = new PVector(); result.sub(PVector.mult(i_Center, i_ZoomFactor)); result.add(new PVector((this.width / 2), (this.height / 2))); return result; }
ae5be590-a762-47ca-9728-fe0ee66e55f6
public void setZoom(float i_ZoomFactor) { this.m_Zoom = i_ZoomFactor; }
fe8a5b24-82b5-4b43-8a0d-637791dd109d
public void setOffset(PVector offset) { this.m_WorldOrigin.set(offset); // System.out.println("World Origin is at " + offset); }
5148a706-f024-4497-9bff-1d252177262a
public void moveToCenter(PVector i_Center, float i_ZoomFactor, int i_TimeToAnimate) { PVector targetOffset = calcWorldOffset(i_Center, i_ZoomFactor); this.m_TimeLeftForCameraAnimation = i_TimeToAnimate; this.m_OriginalAnimationTime = i_TimeToAnimate; this.m_ZoomStep = ((i_ZoomFactor - this.m_Zoom) / i_TimeToAnimate); this.m_OffsetStep = PVector.sub(targetOffset, this.m_WorldOrigin); this.m_OffsetStep.div(i_TimeToAnimate); this.m_OriginalOffset = new PVector(); this.m_OriginalOffset.set(this.m_WorldOrigin); this.m_OriginalZoom = this.m_Zoom; }
b7bc46da-905a-400f-944c-062e64910dfa
public void returnViewToPrevious(int i_TimeToAnimate) { this.m_OffsetStep = PVector.sub(this.m_OriginalOffset, this.m_WorldOrigin); this.m_OffsetStep.div(i_TimeToAnimate); this.m_ZoomStep = ((this.m_OriginalZoom - this.m_Zoom) / i_TimeToAnimate); this.m_TimeLeftForCameraAnimation = i_TimeToAnimate; System.out.println("Original offset " + this.m_OriginalOffset); }
1e67f7f9-6657-4067-b51a-a4f313d365ba
public Component(ManagedPApplet i_Parent) { this.m_Parent = i_Parent; }
7ebec141-89e9-4057-bf6e-51ea23dcd971
public boolean Enabled() { return this.m_Enabled; }