Skip to content

Commit

Permalink
Support min/max in value editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Socolin committed Apr 20, 2024
1 parent 220d864 commit fdd742c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/app/actor/character-sheet-stats.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<fieldset class="stat health">
<legend>{{'Misc.Stat.health' | translate}}</legend>
<div class="value">
<lvl0-relative-value-editor [value$]="characterHealth$" (onChange)="updateHealth($event)">
<lvl0-relative-value-editor [value$]="characterHealth$" [min]="0" [max]="characterMaxHealth$ | async" (onChange)="updateHealth($event)">
{{characterHealth$ | async}} / {{characterMaxHealth$ | async}}
</lvl0-relative-value-editor>
</div>
Expand All @@ -24,7 +24,7 @@
<fieldset class="stat mana">
<legend>{{'Misc.Stat.mana' | translate}}</legend>
<div class="value">
<lvl0-relative-value-editor [value$]="characterMana$" (onChange)="updateMana($event)">
<lvl0-relative-value-editor [value$]="characterMana$" [min]="0" [max]="characterMaxMana$ | async" (onChange)="updateMana($event)">
{{characterMana$ | async}} / {{characterMaxMana$ | async}}
</lvl0-relative-value-editor>
</div>
Expand Down
5 changes: 4 additions & 1 deletion src/app/actor/relative-value-editor.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
</div>
<div class="help-message">
<i class="fa fa-info-circle"></i>
<span>Vous pouvez utiliser des valeur relative tel que '+3' ou '-5' pour augmenter ou réduire la valeur, ou mettre une nouvelle valeur absolue.</span>
<div>
<div>Vous pouvez utiliser des valeur relative tel que '+3' ou '-5' pour augmenter ou réduire la valeur, ou mettre une nouvelle valeur absolue.</div>
<div *ngIf="max">Vous pouvez utiliser <code>min</code> ou <code>max</code> pour mettre la valeur maximum ou minimum.</div>
</div>
</div>
</div>
</div>
Expand Down
36 changes: 29 additions & 7 deletions src/app/actor/relative-value-editor.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import {Component, ElementRef, EventEmitter, Input, NgZone, OnDestroy, OnInit, Output, ViewChild} from '@angular/core';
import {
Component,
ElementRef,
EventEmitter,
Input,
NgZone,
numberAttribute,
OnDestroy,
OnInit,
Output,
ViewChild
} from '@angular/core';
import {Observable, Subscription} from 'rxjs';
import {CdkMenu} from '@angular/cdk/menu';

Expand All @@ -14,6 +25,10 @@ export class RelativeValueEditorComponent implements OnInit, OnDestroy {
value$?: Observable<number>;
@Input('value')
value?: number;
@Input({transform: numberAttribute, alias: 'max'})
max?: number;
@Input('min')
min?: number;
@ViewChild('editField', {static: false})
editField?: ElementRef<HTMLInputElement>
@Input('editable')
Expand Down Expand Up @@ -58,13 +73,20 @@ export class RelativeValueEditorComponent implements OnInit, OnDestroy {
menuContent.menuStack.closeAll();
if (!this.fieldValue)
return;
let newValue = this.valueSnapshot;
if (this.fieldValue.startsWith('+')) {
newValue += parseInt(this.fieldValue.substring(1));
} else if (this.fieldValue.startsWith('-')) {
newValue += parseInt(this.fieldValue);
let newValue = NaN;
if (this.fieldValue === 'max') {
newValue = this.max ?? NaN;
} else if (this.fieldValue === 'min') {
newValue = this.min ?? NaN;
} else {
newValue = parseInt(this.fieldValue);
newValue = this.valueSnapshot;
if (this.fieldValue.startsWith('+')) {
newValue += parseInt(this.fieldValue.substring(1));
} else if (this.fieldValue.startsWith('-')) {
newValue += parseInt(this.fieldValue);
} else {
newValue = parseInt(this.fieldValue);
}
}
if (isNaN(newValue))
return;
Expand Down

0 comments on commit fdd742c

Please sign in to comment.