Added support to change speed of time

This commit is contained in:
Robert Vokac 2024-03-23 20:06:36 +01:00
parent 6f65450f01
commit 9f27e68285
No known key found for this signature in database
GPG Key ID: 693D30BEE3329055
4 changed files with 154 additions and 15 deletions

View File

@ -1,7 +1,6 @@
package org.nanoboot.utils.timecalc.app;
import java.io.File;
import java.io.FileInputStream;
import org.nanoboot.utils.timecalc.entity.Visibility;
import org.nanoboot.utils.timecalc.swing.windows.MainWindow;
import org.nanoboot.utils.timecalc.utils.common.DateFormats;

View File

@ -16,6 +16,8 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Properties;
import lombok.Setter;
import org.nanoboot.utils.timecalc.utils.common.NumberFormats;
/**
* @author Robert Vokac
@ -28,6 +30,8 @@ public class TimeCalcKeyAdapter extends KeyAdapter {
private final MainWindow mainWindow;
private final Time time;
private boolean changeByFiveMinutes = false;
@Setter
private int msToAdd = 1;
public TimeCalcKeyAdapter(
TimeCalcConfiguration timeCalcConfiguration,
@ -61,7 +65,7 @@ public class TimeCalcKeyAdapter extends KeyAdapter {
//meta key ???
}
private void processShifCtrlAltModeKeyCodes(int keyCode, boolean shiftDown,
public void processShifCtrlAltModeKeyCodes(int keyCode, boolean shiftDown,
boolean ctrlDown, boolean altDown) {
if (shiftDown && ctrlDown) {
Utils.showNotification("Following key shortcut is not supported: SHIFT + CTRL");
@ -120,6 +124,36 @@ public class TimeCalcKeyAdapter extends KeyAdapter {
//Utils.showNotification((increase ? "Increasing" : (decrease ? "Decreasing" : "Reseting")) + " millisecond.");
updateProperty(timeCalcConfiguration.testMillisecondCustomProperty, increase, decrease, reset,
Calendar.MILLISECOND);
break;
}
case KeyEvent.VK_U: {
int ms_ = msToAdd;
int s_ = msToAdd / 1000;
ms_ = ms_ - s_ * 1000;
int m_ = msToAdd / 1000 / 60;
ms_ = ms_ - m_ * 1000 * 60;
int h_ = msToAdd / 1000 / 60 / 60;
ms_ = ms_ - h_ * 1000 * 60 * 60;
int d_ = msToAdd / 1000 / 60 / 60 / 24;
ms_ = ms_ - d_ * 1000 * 60 * 60 * 24;
//Utils.showNotification((increase ? "Increasing" : (decrease ? "Decreasing" : "Reseting")) + " second.");
updateProperty(timeCalcConfiguration.testDayCustomProperty, increase, decrease, reset,
Calendar.DAY_OF_MONTH, d_);
updateProperty(timeCalcConfiguration.testHourCustomProperty, increase, decrease, reset,
Calendar.HOUR_OF_DAY, h_);
updateProperty(timeCalcConfiguration.testMinuteCustomProperty, increase, decrease, reset,
Calendar.MINUTE, m_);
updateProperty(timeCalcConfiguration.testSecondCustomProperty, increase, decrease, reset,
Calendar.SECOND, s_);
updateProperty(timeCalcConfiguration.testMillisecondCustomProperty, increase, decrease, reset,
Calendar.MILLISECOND, ms_);
break;
}
case KeyEvent.VK_K: {
@ -130,6 +164,36 @@ public class TimeCalcKeyAdapter extends KeyAdapter {
}
break;
}
case KeyEvent.VK_Q: {
double oldSpeed = this.mainWindow.getSpeed();
if(oldSpeed < 0) {
oldSpeed = 1.0d;
}
if(increase) {
this.mainWindow.increaseSpeed();
}
if(decrease) {
this.mainWindow.decreaseSpeed();
}
if(reset) {
this.mainWindow.resetSpeed();
}
final double newSpeed = this.mainWindow.getSpeed();
if(oldSpeed != newSpeed) {
Utils.showNotification("Speed was changed from " +
((int)oldSpeed) +
" to: " + ((int)newSpeed) + " (" + (NumberFormats.FORMATTER_TWO_DECIMAL_PLACES.format(Math.pow(2, newSpeed))) + ") (" + (newSpeed <= 21 ? TTime.ofMilliseconds(((int)(Math.pow(2,newSpeed) * 1000))) : "many") +" /1s)");
} else {
if(decrease){
Utils.showNotification("Current speed cannot be decreased: " +
NumberFormats.FORMATTER_TWO_DECIMAL_PLACES.format(oldSpeed));
}
}
break;
}
case KeyEvent.VK_A: {
if (increase) {
@ -198,6 +262,14 @@ public class TimeCalcKeyAdapter extends KeyAdapter {
// }
private void updateProperty(IntegerProperty integerProperty,
boolean increase, boolean decrease, boolean reset, int timeUnit) {
updateProperty(integerProperty, increase, decrease, reset, timeUnit, 1);
}
private void updateProperty(IntegerProperty integerProperty,
boolean increase, boolean decrease, boolean reset, int timeUnit, int value) {
if(value == 0) {
//nothing to do
return;
}
int currentValue = integerProperty.getValue();
if ((increase || decrease) && currentValue == Integer.MAX_VALUE) {
@ -235,7 +307,7 @@ public class TimeCalcKeyAdapter extends KeyAdapter {
int oldMinute = cal.get(Calendar.MINUTE);
int oldSecond = cal.get(Calendar.SECOND);
int oldMillisecond = cal.get(Calendar.MILLISECOND);
cal.add(timeUnit, increase ? 1 : (-1));
cal.add(timeUnit, increase ? value : (-value));
int newValue = cal.get(timeUnit);
if (Calendar.MONTH == timeUnit) {
newValue++;
@ -330,7 +402,7 @@ public class TimeCalcKeyAdapter extends KeyAdapter {
}
private void processKeyCode(int keyCode) {
public void processKeyCode(int keyCode) {
boolean onlyGreyOrNone
= timeCalcConfiguration.visibilitySupportedColoredProperty
.isDisabled();
@ -548,6 +620,7 @@ public class TimeCalcKeyAdapter extends KeyAdapter {
timeCalcConfiguration.testSecondCustomProperty.setValue(Integer.MAX_VALUE);
timeCalcConfiguration.testMillisecondCustomProperty.setValue(Integer.MAX_VALUE);
Utils.showNotification(timeCalcConfiguration.print(), 15000, 400);
this.mainWindow.resetSpeed();
break;
}

View File

@ -33,8 +33,10 @@ public class WalkingHumanProgress extends Widget implements
private static final String WALL = "||";
private final Set<Integer> alreadyShownPercents = new HashSet<>();
private static final int LINE_WHERE_HEAD_IS = 2;
private final MainWindow mainWindow;
public WalkingHumanProgress() {
public WalkingHumanProgress(MainWindow mainWindow) {
this.mainWindow = mainWindow;
setFont(new Font(Font.MONOSPACED, Font.PLAIN, 11));
setFocusable(false);
@ -135,14 +137,16 @@ public class WalkingHumanProgress extends Widget implements
} catch (Exception e) {
System.out.println(e);
}
if (image != null) {
// toasterManager.setToasterWidth(600);
// toasterManager.setToasterHeight(400);
toasterManager.showToaster(new ImageIcon(image),
"Progress: " + (percentInt) + "%");
} else {
toasterManager.setToasterHeight(200);
toasterManager.showToaster("Progress: " + (percentInt) + "%");
if (this.mainWindow.getSpeed() == 0) {
if (image != null) {
// toasterManager.setToasterWidth(600);
// toasterManager.setToasterHeight(400);
toasterManager.showToaster(new ImageIcon(image),
"Progress: " + (percentInt) + "%");
} else {
toasterManager.setToasterHeight(200);
toasterManager.showToaster("Progress: " + (percentInt) + "%");
}
}
}

View File

@ -55,6 +55,7 @@ import javax.swing.JCheckBox;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.io.File;
@ -67,6 +68,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.swing.Timer;
import org.nanoboot.utils.timecalc.swing.progress.ProgressDot;
/**
@ -116,6 +118,8 @@ public class MainWindow extends TWindow {
private final IntegerProperty forgetOvertimeProperty = new IntegerProperty("forgetOvertimeProperty", 0);
private WeekStatistics weekStatistics = null;
private final ProgressDot progressDot;
private int speed = Integer.MIN_VALUE;
private final TimeCalcKeyAdapter timeCalcKeyAdapter;
{
ChangeListener valueMustBeTime =
@ -208,7 +212,7 @@ public class MainWindow extends TWindow {
}
Toaster.notificationsVisibleProperty.bindTo(timeCalcConfiguration.notificationsVisibleProperty);
TimeCalcKeyAdapter timeCalcKeyAdapter
this.timeCalcKeyAdapter
= new TimeCalcKeyAdapter(timeCalcConfiguration, timeCalcApp,
this, time);
addKeyListener(timeCalcKeyAdapter);
@ -282,7 +286,7 @@ public class MainWindow extends TWindow {
add(yearBattery);
WalkingHumanProgress walkingHumanProgress
= new WalkingHumanProgress();
= new WalkingHumanProgress(this);
walkingHumanProgress.setBounds(minuteBattery.getX(),
minuteBattery.getY() + minuteBattery.getHeight(), 400, 80);
add(walkingHumanProgress);
@ -820,6 +824,40 @@ public class MainWindow extends TWindow {
workingDayRepository.update(wd);
new Timer(100, e -> {
if(speed == Integer.MIN_VALUE) {
timeCalcConfiguration.testEnabledProperty.setValue(false);
return;
}
if(timeCalcConfiguration.testEnabledProperty.isDisabled()) {
timeCalcConfiguration.testEnabledProperty.enable();
}
if(time.yearCustomProperty.getValue() == Integer.MAX_VALUE) {
time.yearCustomProperty.setValue(time.yearProperty.getValue());
}
if(time.monthCustomProperty.getValue() == Integer.MAX_VALUE) {
time.monthCustomProperty.setValue(time.monthProperty.getValue());
}
if(time.dayCustomProperty.getValue() == Integer.MAX_VALUE) {
time.dayCustomProperty.setValue(time.dayProperty.getValue());
}
if(time.hourCustomProperty.getValue() == Integer.MAX_VALUE) {
time.hourCustomProperty.setValue(time.hourProperty.getValue());
}
if(time.minuteCustomProperty.getValue() == Integer.MAX_VALUE) {
time.minuteCustomProperty.setValue(time.minuteProperty.getValue());
}
if(time.secondCustomProperty.getValue() == Integer.MAX_VALUE) {
time.secondCustomProperty.setValue(time.secondProperty.getValue());
}
if(time.millisecondCustomProperty.getValue() == Integer.MAX_VALUE) {
time.millisecondCustomProperty.setValue(time.millisecondProperty.getValue());
}
int msShouldBeAdded = (int) (Math.pow(2, speed) * 100d);
this.timeCalcKeyAdapter.setMsToAdd(msShouldBeAdded);
this.timeCalcKeyAdapter.processShifCtrlAltModeKeyCodes(KeyEvent.VK_U, true, false, false);
}).start();
while (true) {
if(Math.random() > 0.99) {
@ -1230,4 +1268,29 @@ public class MainWindow extends TWindow {
}
public int getForgetOvertime() {return this.forgetOvertimeProperty.getValue();}
public void setForgetOvertime(int minutes) {this.forgetOvertimeProperty.setValue(minutes);}
public void increaseSpeed() {
if(speed == Integer.MIN_VALUE) {
speed = 0;
}
++this.speed;
}
public void decreaseSpeed() {
if(speed == Integer.MIN_VALUE) {
speed = 0;
}
if(speed == -3){
//nothing to do
return;
}
--this.speed;
}
public int getSpeed() {
return speed;
}
public void resetSpeed() {
this.speed = Integer.MIN_VALUE;
}
}